Handling Request Parameters in Spring Boot

Traditionally, request parameters can be retrieved manually using the HttpServletRequest object. This approach requires explicit parsing and type conversion.

@RestController
public class BasicController {
    @RequestMapping("/manual-param")
    public String manualParam(HttpServletRequest request) {
        String username = request.getParameter("username");
        String ageStr = request.getParameter("age");
        int age = Integer.parseInt(ageStr);
        
        System.out.println("User: " + username + ", Age: " + age);
        return "User: " + username + ", Age: " + age;
    }
}

Spring Boot simplifies this process by automatically binding request parameters to method arguments. The framework handles the type conversion automatically.

@RestController
public class BasicController {
    @RequestMapping("/auto-param")
    public String autoParam(String username, Integer age) {
        System.out.println("User: " + username + ", Age: " + age);
        return "User: " + username + ", Age: " + age;
    }
}

If the request parameter name differs from the method variable name, use the @RequestParam annotation to map them explicitly. You can also configure whether the parameter is required.

@RequestMapping("/mapped-param")
public String mappedParam(@RequestParam(name = "user_login", required = false) String username, Integer age) {
    return "User: " + username + ", Age: " + age;
}

2. Entity Parameters

When handling multiple related parameters, it is cleaner to encapsulate them in a Plain Old Java Object (POJO). Spring Boot will automatically populate the object fields matching the request parameter names.

3. Array Parameters

If a request contains multiple values for the same parameter name (such as multiple checkbox selections), you can receive them as an array.

@RequestMapping("/array-param")
public String arrayParam(String[] ids) {
    System.out.println(Arrays.toString(ids));
    return "IDs: " + Arrays.toString(ids);
}

4. Collection Parameters

To receive multiple values as a List, the @RequestParam annnotation is mandatory to tell Spring how to resolve the collection type.

@RequestMapping("/list-param")
public String listParam(@RequestParam List<string> ids) {
    System.out.println(ids);
    return "IDs: " + ids;
}
</string>

5. Date Parameters

Date parameters require a specific format to be parsed correctly. The @DateTimeFormat annotation defines the expected pattern for the incoming date string.

@RequestMapping("/date-param")
public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime timestamp) {
    System.out.println(timestamp);
    return "Timestamp: " + timestamp;
}

6. JSON Parameters

For sanding complex structured data, clients typically send JSON in the request body. Spring uses the @RequestBody annotation to deserialize this JSON into a Java object.

7. Path Parameters

Parameters can be embedded directly into the URL path. The @PathVariable annotation extracts these values from the URI template.

@RequestMapping("/path/{identifier}")
public String singlePath(@PathVariable Integer identifier) {
    System.out.println(identifier);
    return "ID: " + identifier;
}

Multiple path variables can also be extracted.

@RequestMapping("/path/{identifier}/{category}")
public String multiPath(@PathVariable Integer identifier, @PathVariable String category) {
    System.out.println(identifier + " " + category);
    return "ID: " + identifier + ", Category: " + category;
}

Tags: Spring Boot REST API RequestParam PathVariable Parameter Binding

Posted on Sun, 17 May 2026 09:59:25 +0000 by m5638829