Spring-Boot-9-Header参数获取

Spring 获取Header参数
1. 方法一:对应的部分加入@RequestHeader获取
@Controller
public class HelloController {
@RequestMapping(value = "/hello")
public String hello(@RequestHeader(value="User-Agent") String userAgent){
}
}

2. 方法二:类级别注入HttpServletRequest
@Controller
@RequestMapping("/hello")
public class HelloController {
@Autowired
private HttpServletRequest request;

@RequestMapping(value="/printname/{name}", method=RequestMethod.GET)
public String printName(@PathVariable String name,
@RequestHeader HttpHeaders headers) {
System.out.println("from request:" + request.getHeader("code"));
System.out.println("from parameter:" + headers.getFirst("code"));
return "hello";
}
}
————————————————
版权声明:本文为CSDN博主「SpriCoder」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_44202160/article/details/124265379

posted @ 2022-07-21 14:08  疯子110  阅读(2008)  评论(0编辑  收藏  举报