Spring Boot is a powerful framework for building RESTful web services, and one of its key features is the ability to easily retrieve query parameters from a request. In this hands-on guide, we will walk you through the process of retrieving query parameters in a Spring Boot REST controller using different methods.



Step 1: Create a REST controller

First, let’s create a simple REST controller that has a method that will handle the request. In this example, we will create a REST controller that takes a query parameter called “name” and returns a greeting message.

@RestController
public class GreetingController {

    @GetMapping("/greeting")
    public String greeting(@RequestParam(value = "name") String name) {
        return "Hello, " + name + "!";
    }
}

The @GetMapping annotation maps the request to the greeting() method and the @RequestParam annotation binds the “name” query parameter to the method argument.



Step 2: Retrieve query parameters using @RequestParam

We can use the @RequestParam annotation to retrieve query parameters in a Spring Boot controller. In the above example, we used @RequestParam to retrieve the “name” query parameter and bind it to the “name” method argument.



Step 3: Retrieve query parameters using @PathVariable

Another way to retrieve query parameters in Spring Boot is by using the @PathVariable annotation. This annotation is used to bind a path variable to a method argument. In this example, we will create a REST controller that takes a path variable called “name” and returns a greeting message.

@RestController
public class GreetingController {

    @GetMapping("/greeting/{name}")
    public String greeting(@PathVariable String name) {
        return "Hello, " + name + "!";
    }
}


Step 4: Retrieve query parameters using @RequestBody

In addition to @RequestParam and @PathVariable, we can also use the @RequestBody annotation to retrieve query parameters in a Spring Boot controller. This annotation is used to bind the request body to a method argument. In this example, we will create a REST controller that takes a JSON object containing a “name” property and returns a greeting message.

@RestController
public class GreetingController {

    @PostMapping("/greeting")
    public String greeting(@RequestBody Map<String, String> request) {
        return "Hello, " + request.get("name") + "!";
    }
}


Step 5: Test the REST endpoint

Start your Spring Boot application and test the REST endpoint by sending a GET request to the following URL:

http://localhost:8080/greeting?name=John

You should receive a response with the greeting message “Hello, John!”.



Conclusion

Retrieving query parameters in a Spring Boot REST controller is a simple task that can be achieved by using the @RequestParam, @PathVariable or @RequestBody annotations. With the practical guide provided in this tutorial, you can easily add query parameter support to your RESTful web services.

Note

This is just a basic tutorial on how to retrieve query parameters in Spring Boot REST controller, you can also use other Spring annotation like @PathVariable, @RequestBody etc. depending on your use case and requirement.



Leave a Reply