Sub topic 3
Binding arguments: path, query, headers, and body
This chapter assumes you know Spring MVC's @PathVariable and @RequestParam, and that you can read the TodoClient interface from the earlier chapters. Here you will see how each method argument becomes part of the outgoing HTTP request.
The same annotations, reversed
In Spring MVC, these annotations tell the framework how to read an incoming request. In an HTTP interface they work in reverse. They tell the generated proxy where to put each argument in the request it sends.
| Annotation | Where the value goes |
|---|---|
| `@PathVariable` | Fills a `{placeholder}` in the URL template |
| `@RequestParam` | Adds a query parameter |
| `@RequestHeader` | Adds a request header |
| `@RequestBody` | Becomes the request body |
| `@CookieValue` | Adds a cookie |
An argument with no annotation is not sent as part of the request, unless it is one of the special types described below.
A client that uses all four
import java.util.List;
import java.util.Map;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;
import org.springframework.web.service.annotation.PostExchange;
@HttpExchange(value = "/todos", accept = "application/json")
public interface TodoClient {
@GetExchange("/{id}")
Todo getTodo(@PathVariable long id,
@RequestHeader("X-Trace-Id") String traceId);
@GetExchange
List<Todo> search(@RequestParam long userId,
@RequestParam(required = false) Boolean completed);
@GetExchange
List<Todo> filter(@RequestParam Map<String, String> filters);
@PostExchange(contentType = "application/json")
ResponseEntity<Todo> create(@RequestBody Todo todo);
}
record Todo(Long id, Long userId, String title, boolean completed) {}Calling search(3, true) sends GET /todos?userId=3&completed=true. Calling search(3, null) sends GET /todos?userId=3. The required = false setting is what lets the proxy skip a null value. Without it, the default is required = true, and a null argument fails with an IllegalArgumentException before any request is sent.
Details that cause bugs
- Parameter names. If you write
@PathVariable long idwith no explicit name, Spring reads the nameidfrom the compiled class. That works only when the code is compiled with the-parametersflag. The Spring Boot Maven and Gradle plugins turn it on. In other builds, write@PathVariable("id"). - Collections. A
Listpassed to@RequestParamis sent as repeated parameters, such asids=1&ids=2. - Maps. A
Mapannotated with@RequestParamor@RequestHeaderadds one entry per key. This suits optional filters. - Body serialization. The
@RequestBodyargument is converted by the same message converters as in theRestClient. With Jackson on the classpath, a record becomes JSON. - Static headers. For a header that never changes, use the
acceptandcontentTypeattributes on@HttpExchange. This is simpler than a header argument on every method.
Special argument types
Some argument types need no annotation. A URI argument replaces the URL for that call, which helps when an API returns absolute links such as pagination "next" URLs. An HttpMethod argument replaces the HTTP method.
@GetExchange
List<Todo> followLink(java.net.URI nextPage);Exercise
Write the signature of a method on TodoClient that sends GET /todos?userId=5 and a header X-Trace-Id: abc, and returns a List<Todo>. What happens if the userId argument is a Long and the caller passes null, with no required = false?
Show answer
@GetExchange List<Todo> byUser(@RequestParam Long userId, @RequestHeader("X-Trace-Id") String traceId);. With the default required = true, a null userId fails with an IllegalArgumentException before any request is sent. Adding required = false makes the proxy omit the parameter instead.