SpringMVC4.0以後版本返回json格式數據問題,有什麼偏頗的地方希望大家多多斧正。在這個問題上困擾了我兩天,這兩天翻來覆去睡不著。一直在想這個問題。廢話不多說下面進入正題。
1.創建創建web項目,加入SpringMVC的jar,我這裡演示用spring-framework-4.2.3.RELEASE。jar包如下圖所示:
2.配置web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.配置applicationContext.xml
<?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 http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="cn.xugang.."></context:component-scan>
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManagerFactoryBean"/>
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean" id="contentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false"/>
<property name="favorParameter" value="false"/>
<property name="ignoreAcceptHeader" value="false"/>
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml"/>
<entry key="json" value="application/json"/>
<entry key="xls" value="application/vnd.ms-excel"/>
</map>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" id="mappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4.配置創建controller,使用@Responsebody注解,指定返回內容。
package cn.xugang.cotroller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.xugang.entity.User;
@Controller
@RequestMapping("/user")
public class UserCotroller {
@RequestMapping(value="/hello.html",method=RequestMethod.GET )
public @ResponseBody User hello(){
User user=new User();
user.setAge(19);
user.setName("test");
return user;
}
@RequestMapping(value="/test.html")
public String test(){
System.out.println("hello world");
return "hello";
}
}
5.發送異步請求。這個由於本人用了Etx.js,就直接用ext.js發送異步請求了。
Ext.onReady(function(){
Ext.define('User',{
extend:'Ext.data.Model',
fields:['name','age'],
});
var store=Ext.create('Ext.data.Store',{
model:'User',
proxy:{
type:'ajax',
url:'/SpringMVC4/user/hello.html',
reader:{
type:'json',
}
}
});
store.load({
callback:function(records,operation,success){
if(success){
var msg=[];
store.each(function(user){
msg.push(user.get('name')+" "+user.get('age'));
});
Ext.MessageBox.alert("提示",msg.join("<br>"));
}
}
});
var msg=[];
store.each(function(user){
msg.push(person.get('name')+" "+user.get('age'));
});
Ext.MessageBox.alert('提示',msg.join("<br>"));
});
6.結果演示
7.總結:最後簡單說一下,SpringMVC4.0以上版本用上述這種方式成功返回json,如果是4.0以下,由於json的解析方式不同,這裡也是一個坑,在網上收了很多資料,看了說得是雲裡霧裡,完全無法使用,最後經過自己認真總結得出本文所述內容,4.0以下可使用如下jar包。
並於applicationContext.xml中配置如下代碼
<?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 http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="cn.xugang.."></context:component-scan>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" id="mappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
最後,希望帖子對大家有用,由於第一次寫,很多地方寫的不是很好,請大家多多包涵.如大家對上述有疑惑,可以留言。如轉載請注明出處。謝謝大家!
Spring MVC+Spring3+Hibernate4開發環境搭建 http://www.linuxidc.com/Linux/2013-07/87119.htm
Spring MVC整合Freemarker基於注解方式 http://www.linuxidc.com/Linux/2013-02/79660.htm
基於注解的Spring MVC簡單介紹 http://www.linuxidc.com/Linux/2012-02/54896.htm
Spring MVC 框架搭建及詳解 http://www.linuxidc.com/Linux/2012-01/52740.htm
Spring MVC使用Cron表達式的定時器 http://www.linuxidc.com/Linux/2014-12/110733.htm
簡單的Spring MVC經典案例 http://www.linuxidc.com/Linux/2016-04/129718.htm