SpringMVC返回json数据的三种方式

来源:互联网 发布:asp程序员 编辑:程序博客网 时间:2024/06/06 01:46
方式一:使用JSON工具将对象序列化成json,常用工具Jackson,fastjson,gson。

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


如下:



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


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

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd   
  9.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
  10.           
  11.   
  12.     <!-- 视图解析器 -->  
  13.     <bean id="viewResolver"  
  14.         class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">  
  15.         <property name="mediaTypes">  
  16.             <map>  
  17.                 <entry key="html" value="text/html" />  
  18.                 <entry key="json" value="application/json" />  
  19.                 <entry key="xml" value="application/xml" />  
  20.             </map>  
  21.         </property>  
  22.         <property name="viewResolvers">  
  23.             <list>  
  24.                 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  25.                     <property name="prefix" value="/WEB-INF/views/" />  
  26.                     <property name="suffix" value=".jsp" />  
  27.                 </bean>  
  28.             </list>  
  29.         </property>  
  30.         <property name="defaultViews">  
  31.             <list>  
  32.                 <!-- 不加配置返回 {"account":{"username":"admin","password":"123456"}} -->  
  33.                 <!-- 加配置返回 {"username":"admin","password":"123456"}-->  
  34.                 <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">  
  35.                     <property name="extractValueFromSingleKeyModel" value="true" />   
  36.                 </bean>  
  37.                 <bean class="org.springframework.web.servlet.view.xml.MarshallingView">   
  38.                     <property name="marshaller">   
  39.                         <bean class="org.springframework.oxm.xstream.XStreamMarshaller"/>   
  40.                     </property>   
  41.                 </bean>   
  42.             </list>  
  43.         </property>  
  44.     </bean>  
  45.       
  46. </beans>  




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

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


方式三:注解形式

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans   
  8.         http://www.springframework.org/schema/beans/spring-beans.xsd   
  9.         http://www.springframework.org/schema/context   
  10.         http://www.springframework.org/schema/context/spring-context.xsd   
  11.         http://www.springframework.org/schema/mvc   
  12.         http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
  13.     <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->  
  14.     <mvc:annotation-driven />  
  15.     <!-- use-default-filters="false" 只扫描指定的注解 -->  
  16.     <context:component-scan base-package="com.somnus.controller" use-default-filters="false">  
  17.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  18.     </context:component-scan>  
  19.     <!-- 视图解析器 -->  
  20.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  21.        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  
  22.        <property name="contentType" value="text/html"/>          
  23.        <property name="prefix" value="/WEB-INF/views/"/>  
  24.        <property name="suffix" value=".jsp"/>  
  25.     </bean>  
  26. </beans>  


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


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