Spring Boot @RequestParam Example

1. Introduction

In Spring Boot, the @RequestParam annotation is used to bind query parameters to a method in your controller. This is highly useful when you want to extract data from a query string to use within your method as variables.

Key Points:

1. @RequestParam binds the value of a query string parameter to a method parameter.

2. It is commonly used in Spring MVC web controllers.

3. This annotation has attributes like value, required, and defaultValue to fine-tune the behavior.

2. Implementation Steps

1. Create a Spring Boot project with the Spring Web dependency.

2. Define a controller class annotated with @Controller.

3. Create a handler method using @RequestMapping or one of the HTTP method mapping annotations (@GetMapping, @PostMapping, etc.).

4. Use @RequestParam to bind query parameters to the method's parameters.

5. Optionally, set default values or make parameters optional.

6. Run the application and make a request with query parameters.

3. Implementation Example

Here is the complete code that demonstrates the usage of @RequestParam annotation:
// Step 2: Define a controller to handle web requests
@Controller
public class QueryParameterController {

    // Step 3: Create a method to handle GET requests and extract query parameters
    @GetMapping("/search")
    public ResponseEntity<String> search(@RequestParam String query,
                                         @RequestParam(required = false, defaultValue = "1") int page) {
        // Step 4: Use @RequestParam to bind query parameters
        return ResponseEntity.ok("Searching for: " + query + " on page " + page);
    }

    // Step 5: An example with an optional parameter
    @GetMapping("/greet")
    public ResponseEntity<String> greet(@RequestParam(required = false) String name) {
        String greeting = "Hello, " + (name != null ? name : "Guest");
        return ResponseEntity.ok(greeting);
    }
}

// Step 6: Main class to run the application
@SpringBootApplication
public class RequestParamExampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(RequestParamExampleApplication.class, args);
    }
}

Output:

For /search?query=spring:
Searching for: spring on page 1
For /search?query=spring&page=2:
Searching for: spring on page 2
For /greet:
Hello, Guest
For /greet?name=John:
Hello, John

Explanation:

1. @Controller marks QueryParameterController as a controller component in the Spring MVC framework.

2. @GetMapping("/search") maps GET requests to /search to the search method.

3. @RequestParam String query binds the query query parameter to the method's query parameter.

4. @RequestParam(required = false, defaultValue = "1") int page makes the page parameter optional and defaults to 1 if not provided.

5. @GetMapping("/greet") with @RequestParam(required = false) allows the name parameter to be optional.

6. RequestParamExampleApplication is the Spring Boot main class responsible for booting the application.

7. The output demonstrates how the method parameters are populated based on the provided query parameters. When parameters are not provided, the configured default values or behaviors are applied.


Comments