关于SpringMVC返回json数据的三种方式(转载)

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

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


如下:



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


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

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  5.      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
  6.   
  7.     <description>SpringMVC公共配置</description>  
  8.     <!-- 视图解析器 -->  
  9.     <bean id="viewResolver"  
  10.         class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">  
  11.         <property name="mediaTypes">  
  12.             <map>  
  13.                 <entry key="html" value="text/html" />  
  14.                 <entry key="json" value="application/json" />  
  15.             </map>  
  16.         </property>  
  17.         <property name="viewResolvers">  
  18.             <list>  
  19.                 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  20.                     <property name="prefix" value="/WEB-INF/views/" />  
  21.                     <property name="suffix" value=".jsp" />  
  22.                 </bean>  
  23.             </list>  
  24.         </property>  
  25.         <property name="defaultViews">  
  26.             <list>  
  27.                 <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" >  
  28.                     <property name="extractValueFromSingleKeyModel" value="true" />    
  29.                 </bean>  
  30.             </list>  
  31.         </property>  
  32.     </bean>  
  33. </beans>  


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

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


方式三:注解形式

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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会帮我们做很多事情,那也意味着需要我们自己来配置的越来越少

原文连接:原文连接

猜你喜欢

转载自blog.csdn.net/jiepan9178/article/details/80320663
今日推荐