Bootstrap

spring mvc实现Restful返回json格式数据

使用技术及环境:

  1. Spring 3.2.2.RELEASE
  2. Jackson 1.9.10
  3. JDK 1.6
  4. Eclipse 3.6
  5. Maven 3

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

1、项目依赖

spring和jackson的依赖:

 
 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4. http://maven.apache.org/maven-v4_0_0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.mkyong.common</groupId>
  7. <artifactId>SpringMVC</artifactId>
  8. <packaging>war</packaging>
  9. <version>1.0-SNAPSHOT</version>
  10. <name>SpringMVC Json Webapp</name>
  11. <url>http://maven.apache.org</url>
  12. <properties>
  13. <spring.version>3.2.2.RELEASE</spring.version>
  14. <jackson.version>1.9.10</jackson.version>
  15. <jdk.version>1.6</jdk.version>
  16. </properties>
  17. <dependencies>
  18. <!-- Spring 3 dependencies -->
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring-core</artifactId>
  22. <version>${spring.version}</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-web</artifactId>
  27. <version>${spring.version}</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-webmvc</artifactId>
  32. <version>${spring.version}</version>
  33. </dependency>
  34. <!-- Jackson JSON Mapper -->
  35. <dependency>
  36. <groupId>org.codehaus.jackson</groupId>
  37. <artifactId>jackson-mapper-asl</artifactId>
  38. <version>${jackson.version}</version>
  39. </dependency>
  40. </dependencies>
  41. <build>
  42. <finalName>SpringMVC</finalName>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.apache.maven.plugins</groupId>
  46. <artifactId>maven-eclipse-plugin</artifactId>
  47. <version>2.9</version>
  48. <configuration>
  49. <downloadSources>true</downloadSources>
  50. <downloadJavadocs>false</downloadJavadocs>
  51. <wtpversion>2.0</wtpversion>
  52. </configuration>
  53. </plugin>
  54. <plugin>
  55. <groupId>org.apache.maven.plugins</groupId>
  56. <artifactId>maven-compiler-plugin</artifactId>
  57. <version>2.3.2</version>
  58. <configuration>
  59. <source>${jdk.version}</source>
  60. <target>${jdk.version}</target>
  61. </configuration>
  62. </plugin>
  63. </plugins>
  64. </build>
  65. </project>

2、Model

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

 
 
  1. public class Shop {
  2. String name;
  3. String staffName[];
  4. //getter and setter methods
  5. }

3、Controller

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

  1. Jackson 包已经在项目的 classpath
  2. mvc:annotation-driven注解已经启用
  3. 返回方法已经添加了@ResponseBody

spring会自动处理json的转换。

 
 
  1. import org.springframework.stereotype.Controller;
  2. import org.springframework.web.bind.annotation.PathVariable;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. import com.mkyong.common.model.Shop;
  7. @Controller
  8. @RequestMapping("/kfc/brands")
  9. public class JSONController {
  10. @RequestMapping(value="{name}", method = RequestMethod.GET)
  11. public @ResponseBody Shop getShopInJSON(@PathVariable String name) {
  12. Shop shop = new Shop();
  13. shop.setName(name);
  14. shop.setStaffName(new String[]{"mkyong1", "mkyong2"});
  15. return shop;
  16. }
  17. }

4、mvc:annotation-driven

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

 
 
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  12. <context:component-scan base-package="com.mkyong.common.controller" />
  13. <mvc:annotation-driven />
  14. </beans>

5、示例结果

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

spring-mvc-json-demo

原文链接:https://www.dexcoder.com/selfly/article/3974

;