zoukankan      html  css  js  c++  java
  • springmvc 梳理6--返回json数据 @ResponseBody

    pom.xml

    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.8.sec10</version>
    </dependency>

    例子:

    加上@ResponseBody,return的东西,不会走视图解析器,也就是不会去找视图了,而是将处理的结果放在响应体中,直接返回

    @RequestMapping(value = "/add")
        @ResponseBody
        public String addUser(Integer id,String username,String password,HttpServletRequest request) throws Exception {
        //模型里封装数据
            request.setAttribute("user",new User(id,username,password));
            return "<h1>haha<h1>";
        }

    修改

     

    修改2

    @RequestMapping(value = "/add")
        @ResponseBody
        public String addUser(Integer id,String username,String password,HttpServletRequest request) throws Exception {
        //模型里封装数据
            User user = new User(id,username,password);
            String userStr = JSONObject.toJSONString(user);
            return userStr;
        }

    但是每次这么写还是比较麻烦

    给容器注入一个消息转换的bean

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 自动扫包 -->
        <context:component-scan base-package="com.xinzhi"/>
        <!-- 让Spring MVC不处理静态资源 -->
        <mvc:default-servlet-handler />
    
        <!-- 让springmvc自带的注解生效 -->
        <mvc:annotation-driven >
            <mvc:message-converters>
                <bean id="fastjson"
                      class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
    
    
        <!-- 处理映射器 -->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
        <!-- 处理器适配器 -->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
        <!--视图解析器:DispatcherServlet给他的ModelAndView-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              id="InternalResourceViewResolver">
            <!--前缀-->
            <property name="prefix" value="/WEB-INF/page/"/>
            <!--后缀-->
            <property name="suffix" value=".jsp"/>
        </bean>
    
    
    </beans>
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 自动扫包 -->
        <context:component-scan base-package="com.xinzhi"/>
        <!-- 让Spring MVC不处理静态资源 -->
        <mvc:default-servlet-handler />
    
        <!-- 让springmvc自带的注解生效 -->
        <mvc:annotation-driven >
            <mvc:message-converters>
                <bean id="fastjson"
                      class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
    
    
        <!-- 处理映射器 -->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
        <!-- 处理器适配器 -->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
        <!--视图解析器:DispatcherServlet给他的ModelAndView-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              id="InternalResourceViewResolver">
            <!--前缀-->
            <property name="prefix" value="/WEB-INF/page/"/>
            <!--后缀-->
            <property name="suffix" value=".jsp"/>
        </bean>
    
    
    </beans>
    springmvc-servlet.xml

    测试:

    @RequestMapping(value = "/add")
        @ResponseBody
        public User addUser(Integer id,String username,String password,HttpServletRequest request) throws Exception {
        //模型里封装数据
            return new User(id,username,password);
        }

  • 相关阅读:
    java中static的用法
    java策略设计模式
    java模板设计模式
    Spring 学习笔记 8. 尚硅谷_佟刚_Spring_使用外部属性文件
    Spring 学习笔记 7. 尚硅谷_佟刚_Spring_Bean 的作用域
    Spring学习笔记 6. 尚硅谷_佟刚_Spring_Bean 之间的关系
    Spring学习笔记 5. 尚硅谷_佟刚_Spring_自动装配
    Spring 学习笔记 4. 尚硅谷_佟刚_Spring_属性配置细节
    Spring 学习笔记 3. 尚硅谷_佟刚_Spring_配置 Bean
    Spring 学习笔记 2. 尚硅谷_佟刚_Spring_IOC&DI概述
  • 原文地址:https://www.cnblogs.com/Master-Sun/p/14329931.html
Copyright © 2011-2022 走看看