Spring MVC returns the json format. springmvc returns the json format.
When SpringMVC framework is used to directly return json data to the client, different versions may vary.
The following describes how to configure two types of versions.
Note: Both methods have been verified.
1. Spring3.1.x
1.1 dispatcher-servlet.xml configuration file is as follows:
<? 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: mvc = "http://www.springframework.org/schema/mvc"
Xmlns: context = "http://www.springframework.org/schema/context"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
<Mvc: annotation-driven/>
<! -- Only usable before 3.2 -->
<Bean class = "org. springframework. web. servlet. mvc. annotation. AnnotationMethodHandlerAdapter">
<Property name = "messageConverters">
<List>
<Ref bean = "mappingJacksonHttpMessageConverter"/>
</List>
</Property>
</Bean>
<Bean id = "mappingJacksonHttpMessageConverter" class = "org. springframework. http. converter. json. MappingJacksonHttpMessageConverter">
<Property name = "supportedMediaTypes">
<List>
<Value> application/json; charset = UTF-8 </value>
</List>
</Property>
</Bean>
</Beans>
Pay attention to the highlighted part.
1.2 Add the following dependencies to the pom. xml file:
<Dependency>
<GroupId> org. codehaus. jackson </groupId>
<ArtifactId> jackson-core-asl </artifactId>
<Version> 1.9.13 </version>
</Dependency>
<Dependency>
<GroupId> org. codehaus. jackson </groupId>
<ArtifactId> jackson-mapper-asl </artifactId>
<Version> 1.9.13 </version>
</Dependency>
<Dependency>
<GroupId> org. codehaus. jackson </groupId>
<ArtifactId> jackson-core-lgpl </artifactId>
<Version> 1.9.13 </version>
</Dependency>
<Dependency>
<GroupId> org. codehaus. jackson </groupId>
<ArtifactId> jackson-mapper-lgpl </artifactId>
<Version> 1.9.13 </version>
</Dependency>
2. Spring4.x
2.1 dispatcher-servlet.xml file related configuration:
<? 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: mvc = "http://www.springframework.org/schema/mvc"
Xmlns: context = "http://www.springframework.org/schema/context"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
<! -- <Mvc: annotation-driven/> -->
<Mvc: annotation-driven>
<Mvc: message-converters register-defaults = "true">
<Bean id = "fastJsonHttpMessageConverter" class = "com. alibaba. fastjson. support. spring. FastJsonHttpMessageConverter">
<Property name = "supportedMediaTypes">
<List>
<Value> application/json; charset = UTF-8 </value>
</List>
</Property>
</Bean>
</Mvc: message-converters>
</Mvc: annotation-driven>
</Beans>
Note: spring-mvc-3.2.xsd files need to be used;
2.2 Add the following dependencies to the pom. xml file:
<Dependency>
<GroupId> com. alibaba </groupId>
<ArtifactId> fastjson </artifactId>
<Version> 1.2.6 </version>
</Dependency>