Spring Boot - Query Parameters


π Introduction
In previous article Getting Started with Spring Boot: Your First "Hello, World!" REST API, we created a simple Spring Boot Rest API which returns a string. Now letβs take things a little further and make our application little dynamic by adding query parameters to our HTTP Request.
π οΈ Step 1: Create a Spring Boot Project
π§ Configuration:
Project: Maven
Language: Java
Spring Boot: (Use the latest stable version)
Dependencies: Spring Web
Package name:
com.queryparams
or whatever you like.
The configuration should look like this
Click Generate, unzip the downloaded file, and open it in your favourite IDE (IntelliJ IDEA, Eclipse, VS Code). I prefer to use IntelliJ IDEA.
π Step 2: Understand the Project Structure
src
βββ main
βββ java
β βββ com.queryparams
β βββ QueryParamsApplication.java
βββ resources
βββ application.properties
- We'll now create a
QueryPatamsController.java
to handle HTTP requests.
π¨βπ» Step 3: Create the Controller
package com.amanvw;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class QueryParamsController {
@GetMapping("/hello")
public String sayHello(@RequestParam String name) {
return "Hello, " + name + "!";
}
}
@RequestParam
is the new annotation we introduced here. This annotation is used to extract query parameters, form parameters or any URL parameters from a HTTP Request and bind them to a controller method parameter.So, in our HTTP request when we provide
?name=something
, spring boot will bind it with thesayHello
method parameter βname
.Letβs try it out!
βΆοΈ Step 4: Run the Application
To run the application, you can either
π¦ Use Maven:
./mvnw spring-boot:run
π§ Or use your IDE:
Right-click on QueryParamsApplication.java
β Run.
Youβll see logs ending with:
Tomcat started on port(s): 8080 (http)
π Step 5: Test the Endpoint
Open your browser or Postman and go to:
http://localhost:8080/hello?name=Aman
β Output:
Hello, Aman!
Now, try not providing the query parameter and see what happens. Youβll see an error page.
π§ Understand @RequestParam
When you provide
@RequestParam String name
, you implicitly specify that name is required parameter.To make it not-required use,
@RequestParam(required = false) String name
Also, you can have a different query parameter names in your HTTP Request and the method parameters β
@RequestParam(required = false, name = "name") String firstName
Letβs try this out by adding following method in our QueryParamsController
@GetMapping("/hello-address")
public String sayHelloAddress(@RequestParam String name,
@RequestParam(required = false, name = "address") String place) {
if (place == null) {
return "Hello, " + name + "!";
}
return "Hello, " + name + " from " + place + "!";
}
Stop and start the application again.
Open your browser or Postman and go to:
http://localhost:8080/hello-address?name=Aman&address=India
β Output:
Hello, Aman from India!
Since, address is optional:
http://localhost:8080/hello-address?name=Aman
β Output:
Hello, Aman!
π Conclusion
You just created a simple Spring Boot application that accepts different query parameters and renders response based on those.
You can find the complete code here: GitHub - AmanVW
Stay tuned for the next part in the series!
Β© Aman Walke
Subscribe to my newsletter
Read articles from Aman Walke directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Aman Walke
Aman Walke
I'm a tech enthusiast who loves building backend systems that just work β clean, scalable, and efficient. I've worked with microservices, Spring Boot, Azure, and APIs, and I enjoy digging into root causes and making systems better. Whether it's writing clean code, reviewing it, or managing deployments with DevOps tools, I'm always up for the challenge. I like working in collaborative environments where I can learn, share, and grow alongside smart people.