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:
- Spring 3.0.5.release
- Jackson 1.7.1
- JDK 1.6
- Eclipse 3.6
- 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
- Jackson library existed on classpath
- "MVC: annotation-driven" is enabled
- 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)