添加@EnableWebMvc //配置json转化器 (使用postman)
可以不用写下面两个方法了
@Bean
public RequestMappingHandlerMapping handlerMapping(){
return new RequestMappingHandlerMapping();
}
@Bean
public RequestMappingHandlerAdapter handlerAdapter(){
return new RequestMappingHandlerAdapter();
}
接收数据
接收cookie
@RequestMapping("/data")
public String cookie(@CookieValue(value="cookieName") String value){
return value;
}
@GetMapping("/save")
public String save(HttpServletResponse response){
Cookie cookie=new Cookie("cookieName","root");
response.addCookie(cookie);
return "OK";
}
获取请求头
@RequestMapping("data")
public String data(@RequestHeader("Host") String host){
return "host:"+host;
}
原生对象获取
public String data(HttpServlet httpServlet, HttpServletRequest httpServletRequest,
HttpSession httpSession){
return "HttpServlet"+httpServlet+" HttpServletRequest"+httpServletRequest+" HttpSession"+
httpSession;
}
共享域对象
//1.原生api方式
@Autowired
private ServletContext servletContext;
public void data(HttpServletRequest httpServletRequest, HttpSession httpSession){
}
//2.
//spring提供的方法:request提供了几种 model moaelMap map modelAndView