liveBook · Manning

HTTP request parameter

As explained in Spring Start Here, Second Edition

An HTTP request parameter is a named value supplied with a request, such as in its query string or submitted form data.

An HTTP request parameter sends a small value from client to server as a key-value pair in the request’s query string. The query starts with “?”; separate additional pairs with “&”. section 8.1.1 section 8.1.2

Choose request parameters when:

  • The data is small and may be optional, as with search or filtering criteria. section 8.1.2
  • The value is not mandatory; use a path variable for mandatory values. Use the request body for larger amounts of data. section 8.1.1

In Spring MVC, annotate a controller method parameter with @RequestParam to receive a query value. For example, a request to /home?color=blue can supply “blue” to the controller, which can pass it to the view. section 8.1.2

Listing 8.3 Getting a value through a request parameter
@Controller
public class MainController {
 
  @RequestMapping("/home")
  public String home(
      @RequestParam String color, #A
      Model page) {   #B
    page.addAttribute("username", "Katy");
    page.addAttribute("color", color);    #C
    return "home.html";
  }
}

See how @RequestParam binds a query value to a controller method parameter. section 8.1.2

Figure 8.5 A request parameter can be optional. A common scenario for using request parameters is implementing a search functionality where the search criteria are optional. The client sends only some of the request parameters, and the server knows to use only the values it receives. You implement the server to consider that it might not get values for some of the parameters.

The search example shows how a client can send only the criteria it uses. section 8.1.2

How Spring Start Here, Second Edition covers it

How deep it goes
Defines query-string pairs, optionality, and Spring binding, while deferring larger payloads to request-body handling.
Examples
Uses optional product search criteria and a color value passed from a query string to a controller.
What you can do after
Apply it via Spring MVC’s @RequestParam to receive query values in a controller action.
How it connects
Distinguishes request parameters from path variables, request headers, request bodies, and Spring MVC controller inputs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.