我正在使用spring-boot-1.5.6。我内部有一个控制器,我有大约8个请求映射,这些映射具有公共头。下面提到的标头对于控制器中的大多数方法都是必需的,并且提到所有标头看起来很丑陋,是否有任何方法可以泛化或自定义注释,以免我不想在所有方法中复制标头。
@RestController
public class RestController {
private static final Logger logger = LoggerFactory.getLogger(RestController.class);
/**
* Simply chooses a few headers, logs them and add them to model to
* showHeaders view to render to the user.
*/
@GetMapping(value="/showheaders")
public String getHeaders(
@RequestHeader(value="sample") String sample,
@RequestHeader(value="sample1") String sample1,
@RequestHeader(value="sample2") String sample2,
@RequestHeader(value="sample3") String sample3,
@RequestHeader(value="sample4") String sample4) {
logger.info("Inside getHeaders() method...");
logger.info("sample : " + sample);
logger.info("sample1 : " + sample1);
logger.info("sample2 : " + sample2);
logger.info("sample3 : " + sample3);
logger.info("sample4 : " + sample4);
}
预期:
@RestController
public class RestController {
private static final Logger logger = LoggerFactory.getLogger(RestController.class);
/**
* Simply chooses a few headers, logs them and add them to model to
* showHeaders view to render to the user.
*/
@GetMapping(value="/showheaders")
public String getHeaders(@MyHeaders headers) {
logger.info("Inside getHeaders() method...");
logger.info("sample : " + sample);
logger.info("sample1 : " + sample1);
logger.info("sample2 : " + sample2);
logger.info("sample3 : " + sample3);
logger.info("sample4 : " + sample4);
}
我知道我们可以使用 @RequestHeader Map标头,但是大张旗鼓,我想查看所有标头的参数名称,以便我们可以为方法传递所有标头。使用地图时不显示任何内容。
任何提示或建议都是可以理解的。
答案 0 :(得分:2)
您可以为此使用@ModelAttribute
:
public class MyheaderHolder {
private String sample;
private String sample1;
private String sample2;
private String sample3;
private String sample4;
//getter setters
}
@ModelAttribute("myHeaderHolder")
public Something addSomething(@RequestHeader(value = "sample") String sample,
@RequestHeader(value = "sample1") String sample1,
@RequestHeader(value = "sample2") String sample2,
@RequestHeader(value = "sample3") String sample3,
@RequestHeader(value = "sample4") String sample4) {
return new MyHeaderHolder(name);
}
@GetMapping(value = "/showheaders")
public String getHeaders(@ModelAttribute MyHeaderHolder myHeaderHolder) {
logger.info("Inside getHeaders() method...");
logger.info("sample : " + myHeaderHolder.getSample());
logger.info("sample1 : " + myHeaderHolder.getSample1());
logger.info("sample2 : " + myHeaderHolder.getSample2());
logger.info("sample3 : " + myHeaderHolder.getSample3());
logger.info("sample4 : " + myHeaderHolder.getSample4());
}
答案 1 :(得分:-1)
您可以像这样使用@RequestHeader
@GetMapping(value="/showheaders")
public String getHeaders(@RequestHeader Map<String, String> myHeaders) {
logger.info("Inside getHeaders() method...");
logger.info("sample : " + myHeaders.get("sample"));
logger.info("sample1 : " + myHeaders.get("sample1"));
logger.info("sample2 : " + myHeaders.get("sample2"));
logger.info("sample3 : " + myHeaders.get("sample3"));
logger.info("sample4 : " + myHeaders.get("sample4"));
}
请求标头在地图中设置。你可以得到你需要的东西。