编程开发 > JSP > 文章内容

Springmvc实现Restful返回json格式数据实例详解

2017-4-29编辑:ljnbset

Spring mvc实现Restful返回json格式数据实例详解

在本示例中,我们将向您展示如何将对象转换成json格式并通过spring mvc框架返回给用户。

使用技术及环境:

  • Spring 3.2.2.RELEASE
  • Jackson 1.9.10
  • JDK 1.6
  • Eclipse 3.6
  • Maven 3

PS:在spring 3 中,要输出json数据,只需要添加Jackson 库到你的classpath。

1、项目依赖

spring和jackson的依赖:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

<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.0modelVersion>

 <groupId>com.mkyong.commongroupId>

 <artifactId>SpringMVCartifactId>

 <packaging>warpackaging>

 <version>1.0-SNAPSHOTversion>

 <name>SpringMVC Json Webappname>

 <url>http://maven.apache.orgurl>

 <properties>

 <spring.version>3.2.2.RELEASEspring.version>

 <jackson.version>1.9.10jackson.version>

 <jdk.version>1.6jdk.version>

 properties>

 <dependencies>

 

 <dependency>

  <groupId>org.springframeworkgroupId>

  <artifactId>spring-coreartifactId>

  <version>${spring.version}version>

 dependency>

 <dependency>

  <groupId>org.springframeworkgroupId>

  <artifactId>spring-webartifactId>

  <version>${spring.version}version>

 dependency>

 <dependency>

  <groupId>org.springframeworkgroupId>

  <artifactId>spring-webmvcartifactId>

  <version>${spring.version}version>

 dependency>

 

 <dependency>

  <groupId>org.codehaus.jacksongroupId>

  <artifactId>jackson-mapper-aslartifactId>

  <version>${jackson.version}version>

 dependency>

 dependencies>

 <build>

 <finalName>SpringMVCfinalName>

 <plugins>

  <plugin>

  <groupId>org.apache.maven.pluginsgroupId>

  <artifactId>maven-eclipse-pluginartifactId>

  <version>2.9version>

  <configuration>

  <downloadSources>truedownloadSources>

  <downloadJavadocs>falsedownloadJavadocs>

  <wtpversion>2.0wtpversion>

  configuration>

  plugin>

  <plugin>

  <groupId>org.apache.maven.pluginsgroupId>

  <artifactId>maven-compiler-pluginartifactId>

  <version>2.3.2version>

  <configuration>

  <source>${jdk.version}source>

  <target>${jdk.version}target>

  configuration>

  plugin>

 plugins>

 build>

project>

2、Model

一个简单的JavaBean,稍后将被转换成json格式输出。

?

1

2

3

4

5

public class Shop {

 String name;

 String staffName[];

 //getter and setter methods

}

3、Controller

添加@ResponseBody到返回值,我们看到:

Jackson 包已经在项目的 classpath

mvc:annotation-driven注解已经启用

返回方法已经添加了@ResponseBody

spring会自动处理json的转换。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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](https://my.oschina.net/u/1774615)

@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

在你的spring配置文件中启用mvc:annotation-driven注解。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<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、示例结果

访问URL:http://localhost:8080/SpringMVC/rest/kfc/brands/kfc-kampar

spring-mvc-json-demo

Springmvc实现Restful返回xml格式数据实例详解

热点推荐

登录注册
触屏版电脑版网站地图