Spring MVC returns the json format. springmvc returns the json format.

Source: Internet
Author: User

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>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.