SpringMVC-02

添加@EnableWebMvc //配置json转化器 (使用postman)

可以不用写下面两个方法了

@Bean

public RequestMappingHandlerMapping handlerMapping(){

return new RequestMappingHandlerMapping();

}

@Bean

public RequestMappingHandlerAdapter handlerAdapter(){

return new RequestMappingHandlerAdapter();

}

接收数据

接收cookie

java 复制代码
    @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";
    }

获取请求头

java 复制代码
    @RequestMapping("data")
    public String data(@RequestHeader("Host") String host){
        return "host:"+host;
    }

原生对象获取

java 复制代码
    public String data(HttpServlet httpServlet, HttpServletRequest httpServletRequest,
                       HttpSession httpSession){
        return "HttpServlet"+httpServlet+" HttpServletRequest"+httpServletRequest+" HttpSession"+
                httpSession;
    }

共享域对象

java 复制代码
//1.原生api方式	
    @Autowired
    private ServletContext servletContext;
    public void data(HttpServletRequest httpServletRequest, HttpSession httpSession){
        
    }
    
 //2.
    //spring提供的方法:request提供了几种   model   moaelMap    map    modelAndView
相关推荐
亲爱的马哥1 分钟前
重磅更新 | 填鸭表单TDuckX2.9发布!
java
Java中文社群2 分钟前
26届双非上岸记!快手之战~
java·后端·面试
whitepure7 分钟前
万字详解Java中的面向对象(二)——设计模式
java·设计模式
whitepure9 分钟前
万字详解Java中的面向对象(一)——设计原则
java·后端
2301_7930868737 分钟前
SpringCloud 02 服务治理 Nacos
java·spring boot·spring cloud
回家路上绕了弯44 分钟前
MySQL 详细使用指南:从入门到精通
java·mysql
小七rrrrr1 小时前
动态规划法 - 53. 最大子数组和
java·算法·动态规划
自由的疯1 小时前
在 Java IDEA 中使用 DeepSeek 详解
java·后端·架构
自由的疯1 小时前
Java 通过接口方式使用 DeepSeek 详解
java·后端·trae