Spring 3 MVC and JSON example

Source: Internet
Author: User

In spring 3, you can enable "MVC: annotation-driven" to support object conversion to/from JSON format, if Jackson JSON processor is existed on the project classpath.

In this tutorial, we show you how to output JSON data from spring MVC.

Technologies used:

  1. Spring 3.0.5.release
  2. Jackson 1.7.1
  3. JDK 1.6
  4. Eclipse 3.6
  5. Maven 3
1. Project Dependencies

To use JSON in spring MVC, You need to includeJacksonDependency.

<Properties>
<Spring. version> 3.0.5.release </spring. version>
</Properties>
 
<Dependencies>
 
<! -- Jackson JSON mapper -->
<Dependency>
<Groupid> org. codehaus. Jackson </groupid>
<Artifactid> Jackson-mapper-Asl </artifactid>
<Version> 1.7.1 </version>
</Dependency>
 
<! -- Spring 3 dependencies -->
<Dependency>
<Groupid> org. springframework </groupid>
<Artifactid> spring-core </artifactid>
<Version >$ {spring. Version} </version>
</Dependency>
 
<Dependency>
<Groupid> org. springframework </groupid>
<Artifactid> spring-Web </artifactid>
<Version >$ {spring. Version} </version>
</Dependency>
 
<Dependency>
<Groupid> org. springframework </groupid>
<Artifactid> spring-webmvc </artifactid>
<Version >$ {spring. Version} </version>
</Dependency>
 

</Dependencies>

2. Model

A simple pojo, later convert this object into JSON output.

Package com. mkyong. Common. model;
 
Public class shop {
 
String name;
String staffname [];
 
// Getter and setter Methods
 

}

3. Controller

Add"@ Responsebody"In the return value, no much detail in the spring documentation.

As I know, when spring see

  1. Jackson library existed on classpath
  2. "MVC: annotation-driven" is enabled
  3. Return method annotated with @ responsebody

It will handle the JSON conversion automatically.

Package com. mkyong. Common. Controller;
 
Import org. springframework. stereotype. Controller;
Import org. springframework. Web. Bind. annotation. pathvariable;
Import org. springframework. Web. Bind. annotation. requestmapping;
Import org. springframework. Web. Bind. annotation. requestmethod;
Import org. springframework. Web. Bind. annotation. responsebody;
Import com. mkyong. Common. model. shop;
 
@ Controller
@ Requestmapping ("/KFC/Brands ")
Public class jsoncontroller {
 
@ Requestmapping (value = "{name}", method = requestmethod. Get)
Public @ responsebody shop getshopinjson (@ pathvariable string name ){
 
Shop shop = new shop ();
Shop. setname (name );
Shop. setstaffname (New String [] {"mkyong1", "mkyong2 "});
 
Return shop;
 
}
 

}

4. MVC: annotation-driven

Enable"MVC: annotation-driven"In your spring configuration XML file.

<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: MVC = "http://www.springframework.org/schema/mvc"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation ="
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd>
 
<Context: component-scan base-package = "com. mkyong. Common. Controller"/>
 
<MVC: annotation-driven/>
 

</Beans>

5. Demo

URL:Http: // localhost: 8080/springmvc/rest/KFC/brands/KFC-kampar

Download source codedownload it-SpringMVC-JSON-Example.zip (7 KB)

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.