spring mvc获取header

摘自:http://www.cnblogs.com/lijc1990/p/3635780.html   (原文链接:http://viralpatel.net/blogs/spring-requestheader-example/

spring mvc获取header

两种方法: 
1.在方法参数中加入@RequestHeader 
2.在类级别注入HttpServletRequest 
建议使用第二种方法,这样可避免每个方法都加入HttpHeaders参数 

复制代码
 1 @Controller
 2 @RequestMapping("/hello")
 3 public class HelloController {
 4      @Autowired
 5      private HttpServletRequest request;
 6     
 7      @RequestMapping(value="/printname/{name}", method=RequestMethod.GET)
 8      public String printName(@PathVariable String name,
 9               @RequestHeader HttpHeaders headers) {
10           System.out.println("from request:" + request.getHeader("code"));
11           System.out.println("from parameter:" + headers.getFirst("code"));
12          
13           return "hello";
14      }
15 }
复制代码

 

复制代码
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
//..
 
@Controller
public class HelloController {
 
    @RequestMapping(value = "/hello.htm")
    public String hello(@RequestHeader(value="User-Agent") String userAgent)
 
        //..
    }
}
复制代码
posted @ 2018-09-29 09:23  长久悠悠  阅读(8486)  评论(0编辑  收藏  举报