问题描述
我有一个带有多个控制器的 Spring Boot 应用程序,可提供各种 REST 方法.每种方法都要求定义相同的标头参数.有没有办法为所有控制器方法一次性指定类似以下内容?
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, ...) {
...
}
谢谢.
推荐答案
你或许可以使用 @ModelAttribute
来实现,像这样:
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) {
//...
}
您可以在单个 Controller 或 @ControllerAdvice
类中实现 @ModelAttribute
填充方法,以辅助多个控制器.见参考文档.
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.
这篇关于为 Spring Boot 应用程序中的所有控制器指定一次 @RequestHeader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!