Hackforge Academy

Category: spring_boot

query param in spring boot

Published on 17 Jun 2026

Explanation

Query parameters are key-value pairs sent in the URL after a '?' symbol. In Spring Boot, the @RequestParam annotation is used to read query parameters from HTTP requests.

Code:

@GetMapping("/hello")
public String hello(
@RequestParam String name) {
    return "Hello " + name;
}

// Request: GET /hello?name=Praveen

Explanation

Query parameters can have default values. If the client does not provide the parameter, the default value is used.

Code:

@GetMapping("/welcome")
public String welcome(
@RequestParam(defaultValue = "Guest") 
String name) {
    return "Welcome " + name;
}

// Request: GET /welcome
// Response: Welcome Guest

Explanation

Query parameters can be marked as optional using required=false. If the parameter is missing, Spring Boot assigns null.

Code:

@GetMapping("/user")
public String getUser(
@RequestParam(required = false) String name)
 {
    return name == null ? 
"User not provided" : name;
}

// Request: GET /user

Explanation

Multiple query parameters can be received in a single API using multiple @RequestParam annotations.

Code:

@GetMapping("/add")
public int add(@RequestParam int a,
 @RequestParam int b) {
    return a + b;
}

// Request: GET /add?a=10&b=20
// Response: 30

Explanation

Query parameters are commonly used for filtering, searching, pagination, and sorting data in REST APIs.

Code:

@GetMapping("/products")
public String getProducts(
@RequestParam String category,
                         
 @RequestParam int page) {
    return "Category: " +
 category + ", Page: " + page;
}

// Request: GET /products?category=
mobile&page=1

๐Ÿš€ Learn Spring Boot with real-world projects

๐Ÿ’ก Build REST APIs step by step

๐Ÿง  Improve backend development skills

๐ŸŽฏ Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

๐Ÿ

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
โš›๏ธ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
โ˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community