问题
I have a Spring Boot app with multiple controllers serving various REST methods. Each of the methods require that the same header parameter be defined. Is there a way to specify something like the following one time for all controller methods?
public ResponseEntity get(@RequestHeader(value="NAME", required = true) String name, ...) {
...
}
Thanks.
回答1:
You can probably achieve this using @ModelAttribute
, like this:
public class Something {
private name;
//...
}
@ModelAttribute("something")
public Something addSomething(@RequestHeader(value="NAME", required = true) String name) {
return new Something(name);
}
@RequestMapping("/something")
public ResponseEntity get(@ModelAttribute Something something) {
//...
}
You can implement the @ModelAttribute
populating method in a single Controller or in a @ControllerAdvice
class, in order to assist multiple controllers. See reference documentation.
来源:https://stackoverflow.com/questions/31497786/specifying-requestheader-once-for-all-controllers-in-spring-boot-app