Handling Request Parameters in Spring MVC

Spring MVC lets developers retrieve request data using the raw HttpServletRequest, or more conveniently with annotations such as @RequestParam, @RequestHeader, @RequestBody, and @CookieValue, supports automatic binding to POJOs including nested objects, injects native Servlet objects like HttpSession, and handles character encoding via CharacterEncodingFilter.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
Handling Request Parameters in Spring MVC

This article demonstrates how Spring MVC processes request parameters, covering the use of various annotations and native Servlet API objects.

Servlet style : parameters are retrieved with HttpServletRequest.getParameter("name"). Example:

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // Get parameter value
        String username = req.getParameter("username");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

@RequestParam : directly bind a request parameter to a method argument.

@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello(String username) {
        System.out.println(username);
        return "success";
    }
}

When the parameter name differs from the method argument, specify it with @RequestParam("user"). The required attribute can be set to false to avoid errors, and defaultValue can provide a fallback.

@RequestMapping("/hello")
public String hello(@RequestParam(value = "user", required = false, defaultValue = "zhang") String username) {
    System.out.println(username);
    return "success";
}

@RequestHeader : obtain HTTP header values.

@RequestMapping("/hello")
public String hello(@RequestHeader("User-Agent") String userAgent) {
    System.out.println(userAgent);
    return "success";
}

Set required = false or provide defaultValue to handle missing headers.

@RequestMapping("/hello")
public String hello(@RequestHeader(value = "test", required = false) String userAgent) {
    System.out.println(userAgent);
    return "success";
}

@RequestBody : bind the request body (e.g., JSON) to a method argument or a POJO.

@RequestMapping("/hello")
public String hello(@RequestBody String json) {
    System.out.println(json);
    return "success";
}

Or bind directly to a POJO:

@RequestMapping("/hello")
public String hello(@RequestBody Person person) {
    System.out.println(person);
    return "success";
}

@CookieValue : retrieve cookie values.

@RequestMapping("/hello")
public String hello(@CookieValue("_ga") String ga) {
    System.out.println(ga);
    return "success";
}

Object binding : complex parameters can be bound to a Java object, supporting nested objects via dot notation.

@Data
public class Person {
    private String name;
    private Integer age;
    private Character sex;
    private Address address;
}

@Data
public class Address {
    private String province;
    private String city;
}

Controller method:

@RequestMapping("/hello")
public String hello(Person person) {
    System.out.println(person);
    return "success";
}

Native Servlet API objects : HttpServletResponse, HttpSession, etc., can be injected directly.

@RequestMapping("/hello")
public String hello(HttpServletResponse response) {
    Cookie cookie = new Cookie("test", "test");
    response.addCookie(cookie);
    return "success";
}

@RequestMapping("/hello")
public String hello(HttpSession session) {
    session.setAttribute("test", "test");
    return "success";
}

Character encoding : use CharacterEncodingFilter in web.xml to solve request/response garbled text.

<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

These examples cover the full range of request‑parameter handling techniques in Spring MVC.

JavaBackend DevelopmentAnnotationsRequest ParametersSpring MVC
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.