Spring MVC transmits JSON data with Ajax

Source: Internet
Author: User

In Spring MvC3, Jackson (JSON processor) has been integrated to handle the data output JSON format, and the classes encapsulated in spring are Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter This JSON converter, if it's a previous version of springmvc3.2, You can use the Org.springframework.http.converter.MappingJacksonHttpMessageConverter JSON converter.


Spring's detailed description: here

A detailed description of JSON: here

A detailed description of Spring MVC: here



1. To add Maven project dependencies first, to use JSON in spring MVC, you need to include Jackson's dependencies in the Pom.xml in your project.

Example:

 <dependencies> <dependency> <groupId>com.fasterxml</groupId> <artifactid>jackson- annotations</artifactid> <version>2.1.1</version> </dependency> <dependency> &lt ;groupid>com.fasterxml</groupid> <artifactId>jackson-core</artifactId> <version>2.1.1 </version> </dependency> <dependency> <groupId>com.fasterxml</groupId> <artif Actid>jackson-databind</artifactid> <version>2.1.1</version> </dependency> <!-- Spring 3 Dependencies-<dependency> <groupId>org.springframework</groupId> <artifact Id>spring-aop</artifactid> <version>3.2.0.RELEASE</version> </dependency> < Dependency> <groupId>org.springframework</groupId> <artifactid>spring-aspects</artifacti D> <version>3.2.0.release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.2.0.RELEASE</version> </dependency&  Gt <dependency> <groupId>org.springframework</groupId> <artifactid>spring-context</artif actid> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupid>o Rg.springframework</groupid> <artifactId>spring-context-support</artifactId> <version>3. 2.0.release</version> </dependency> <dependency> <groupid>org.springframework</groupid > <artifactId>spring-core</artifactId> <version>3.2.0.RELEASE</version> </depende ncy> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-expressi   On</artifactid>   <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupid>org.springfra Mework</groupid> <artifactId>spring-instrument</artifactId> <version>3.2.0.release</v ersion> </dependency> <dependency> <groupId>org.springframework</groupId> <artif Actid>spring-instrument-tomcat</artifactid> <version>3.2.0.RELEASE</version> </ dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-j dbc</artifactid> <version>3.2.0.RELEASE</version> </dependency> <dependency> &lt ;groupid>org.springframework</groupid> <artifactId>spring-jms</artifactId> <version>3 .2.0.release</version> </dependency> <dependency> <groupid>org.springframework</ Groupid> <artifactid>spring-orm</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <gr Oupid>org.springframework</groupid> <artifactId>spring-oxm</artifactId> <version>3.2. 0.release</version> </dependency> <dependency> <groupid>org.springframework</groupid&      Gt <artifactId>spring-tx</artifactId> <version>3.2.0.RELEASE</version> </dependency> & Lt;dependency> <groupId>org.springframework</groupId> <artifactid>spring-web</artifactid > <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupid>org.sp Ringframework</groupid> <artifactId>spring-webmvc</artifactId> <version>3.2.0.release&lt ;/version> </dependency> </dependencies>

2. Create a Pojo class, take the user class as an example, here to paste the user class code, we all understand.


3. Create a controller with @controller annotations

Plus @responsebody.

The code looks like this:

Import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.ResponseBody; Import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import com. Student; @Controllerpublic class Testjsoncontroller {          @RequestMapping (value= "/finddata", method = requestmethod.get) public     @ResponseBody Student getjsondata () {            Student result = new Student ();         Result.setusername ("360sdn.com");         Result.setarray (New string[]{"360sdn1", "360SDN2"});            return result;}        }

4. Configure the spring servlet configuration file to configure the JSON output converter, but in my practice I find that it is possible to convert the data into JSON format without the beans in the configuration 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 /MVC http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-3.1.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-3.1.xsd "> <!--start the Spring MVC annotation feature, Complete the mapping of requests and annotations Pojo-<!--configuration information conversion, convert the return value of the @responsebody annotation to JSON to return to the foreground, encoded as utf-8--> <bean class= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "> <property name="              Messageconverters "> <list> <bean       class= "Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" > <pro Perty name= "Supportedmediatypes" > <list> <value>applica                 Tion/json;charset=utf-8</value> </list> </property> </bean> </list> </property> </bean> <mvc:annotation-driven /></beans>



You should then be able to see the JSON-formatted data in the run.

Spring MVC transmits JSON data with Ajax

Related Article

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.