SpringMVC返回json数据的三种方式

xiaoxiao2021-02-28  36

方式一:使用JSON工具将对象序列化成json,常用工具Jackson,fastjson,gson

利用HttpServletResponse,然后获取response.getOutputStream()或response.getWriter(),直接输出。

如下:

这种方式最为直接,但是在既然已经用了SpingMVC框架的情况下,再用这种方式,有点不合时宜,out啦。

方式二:非注解形式,配置JsonView视图

[html]  view plain  copy  print ? <?xml version="1.0" encoding="UTF-8" standalone="no"?>   <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 http://www.springframework.org/schema/context/spring-context.xsd            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">                     <!-- 视图解析器 -->       <bean id="viewResolver"           class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">           <property name="mediaTypes">               <map>                   <entry key="html" value="text/html" />                   <entry key="json" value="application/json" />                   <entry key="xml" value="application/xml" />               </map>           </property>           <property name="viewResolvers">               <list>                   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">                       <property name="prefix" value="/WEB-INF/views/" />                       <property name="suffix" value=".jsp" />                   </bean>               </list>           </property>           <property name="defaultViews">               <list>                   <!-- 不加配置返回 {"account":{"username":"admin","password":"123456"}} -->                   <!-- 加配置返回 {"username":"admin","password":"123456"}-->                   <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">                       <property name="extractValueFromSingleKeyModel" value="true" />                    </bean>                   <bean class="org.springframework.web.servlet.view.xml.MarshallingView">                        <property name="marshaller">                            <bean class="org.springframework.oxm.xstream.XStreamMarshaller"/>                        </property>                    </bean>                </list>           </property>       </bean>          </beans>  

那么我们的访问方式应该为:http://localhost:8080/SpringMVC/account/viewResolver.json

如果我们想以xml的形式返回,当然还要配xml视图,那相应的访问路劲为:http://localhost:8080/SpringMVC/account/viewResolver.xml

方式三:注解形式

[html]  view plain  copy  print ? <?xml version="1.0" encoding="UTF-8" standalone="no"?>   <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            http://www.springframework.org/schema/context/spring-context.xsd            http://www.springframework.org/schema/mvc            http://www.springframework.org/schema/mvc/spring-mvc.xsd">       <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->       <mvc:annotation-driven />       <!-- use-default-filters="false" 只扫描指定的注解 -->       <context:component-scan base-package="com.somnus.controller" use-default-filters="false">           <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>       </context:component-scan>       <!-- 视图解析器 -->       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">          <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>          <property name="contentType" value="text/html"/>                  <property name="prefix" value="/WEB-INF/views/"/>          <property name="suffix" value=".jsp"/>       </bean>   </beans>  

由于配置了<mvc:annotation-driven />,SpringMVC会帮我们做很多事情,那也意味着需要我们自己来配置的越来越少,至于做了哪些事情可以看我的这篇博文《 SpringMVC 解读——<mvc:annotation-driven/>》。

是不是使用方式越来越简单了呢,程序员越来越傻,不知道是好事,还是坏事……

转载请注明原文地址: https://www.6miu.com/read-75038.html

最新回复(0)