Spring3 MVC Consolidation JSON Instance __json

Source: Internet
Author: User
Tags xml example

Original link: http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/

In a previous article about how Spring MVC consolidates JSON and returns JSON format, this article is very concise, so just translate it a little bit and reprint it.

=============================================================================================================== =====================

This tutorial will show you how to return JSON-formatted content in the Spring MVC framework.

Technology used: Spring 3.2.2.RELEASE Jackson 1.9.10 JDK 1.6 Eclipse 3.6 Maven 3

PS: In spring 3, just put the Jackson bag under the classpath root directory. 1. Project Dependency Configuration (Note: This part is relative to the MAVEN project, if it is not a MAVEN managed project, you can ignore this part, directly to the relevant Sring package and Jackson package into the Lib library can be.) )

Configure Jackson and Spring dependencies

Pom.xml is as follows

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>com.mkyong.common</groupId> <artifactid>springmvc</ artifactid> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name> Springmvc Json webapp</name> <url>http://maven.apache.org</url> <properties> <spring.ver Sion>3.2.2.release</spring.version> <jackson.version>1.9.10</jackson.version> < Jdk.version>1.6</jdk.version> </properties> <dependencies> <!--Spring 3 dependencies--&G
		T <dependency> <groupId>org.springframework</groupId> <artifactid>spring-core</ Artifactid> &LT;VERSION&GT;${SPRING.VERSION}&LT;/VERSION&GT </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spri 
			ng-web</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <versio
			N>${spring.version}</version> </dependency> <!--Jackson JSON Mapper--> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> < version>${jackson.version}</version> </dependency> </dependencies> <build> <finaln ame>springmvc</finalname> <plugins> <plugin> <groupid>org.apache.maven.plugins</gro upid> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configu Ration> <doWnloadsources>true</downloadsources> <downloadJavadocs>false</downloadJavadocs> < wtpversion>2.0</wtpversion> </configuration> </plugin> <plugin> <groupid>or G.apache.maven.plugins</groupid> <artifactId>maven-compiler-plugin</artifactId> <version >2.3.2</version> <configuration> <source>${jdk.version}</source> &LT;TARGET&GT;${JDK .version}</target> </configuration> </plugin> </plugins> </build> &LT;/PROJECT&G T
2. Model

A simple Pojo object that will later be converted to JSON-formatted data output.

Package Com.mkyong.common.model;
 
public class Shop {
 
	String name;
	String staffname[];
 
	Getter and Setter Methods
 
}
  3. Controller

Add @responsebody annotations before the method name. If the Spring Discovery Project Lib Library contains the Jackson package spring enabled the Mvc:annotation-driven MVC annotation Configuration return method using the @responsebody annotation

Spring automatically transforms the JSON.

The Jsoncontroller.java is as follows:

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 annotation

Enable Mvc:annotation-driven annotations in your spring configuration

Mvc-dispatcher-servlet.xml:

<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. Examples

Url:http://localhost:8080/springmvc/rest/kfc/brands/kfc-kampar

Http://www.mkyong.com/wp-content/uploads/2011/07/spring-mvc-json-demo.png

sample source Download: Download It–springmvc-json-example.zip (KB)

Reference: Mvc-annotation-driven documentation high-performance JSON processor Spring MVC and XML example tags:jacks On JSON spring MVC Spring3

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.