springmvc 获取到header怎么返回
1个回答
展开全部
建议使用这种方法,这样可避免每个方法都加入HttpHeaders参数
Java代码
@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";
}
}
Java代码
@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";
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询