原文: https://howtodoinjava.com/spring-restful/spring-rest-hello-world-json-example/

Spring REST JSON 示例中,我们将学习编写能够返回资源的 JSON 表示形式的 RESTful Web 服务。 我们将使用MappingJackson2JsonView将视图解析为 JSON 正文。

阅读更多: Spring REST XML 教程

1. Spring REST JSON – @ResponseBody注解

首要技术简单易行。 我们只需要在应用程序的类路径中包含 jackson 依赖项,spring 就会自动将Jackson2JsonMessageConverter类注册到上下文中。 每当我们从 REST API 请求资源并提供 http 标头accept: application/json时,我们都将取回资源的 json 表示形式。

1.1. JSON 运行时依赖

pom.xml

  1. <!-- Jackson JSON Processor -->
  2. <dependency>
  3. <groupId>com.fasterxml.jackson.core</groupId>
  4. <artifactId>jackson-databind</artifactId>
  5. <version>2.4.1</version>
  6. </dependency>

1.2. @ResponseBody支持

这里@RestController = @Controller + @ResponseBody

EmployeeRESTController.java

  1. @RestController
  2. public class EmployeeRESTController
  3. {
  4. @RequestMapping(value = "/employees")
  5. public EmployeeListVO getAllEmployees()
  6. {
  7. EmployeeListVO employees = new EmployeeListVO();
  8. EmployeeVO empOne = new EmployeeVO(1,"Lokesh","Gupta","howtodoinjava@gmail.com");
  9. EmployeeVO empTwo = new EmployeeVO(2,"Amit","Singhal","asinghal@yahoo.com");
  10. EmployeeVO empThree = new EmployeeVO(3,"Kirti","Mishra","kmishra@gmail.com");
  11. employees.getEmployees().add(empOne);
  12. employees.getEmployees().add(empTwo);
  13. employees.getEmployees().add(empThree);
  14. return employees;
  15. }
  16. @RequestMapping(value = "/employees/{id}")
  17. public ResponseEntity<EmployeeVO> getEmployeeById (@PathVariable("id") int id)
  18. {
  19. if (id <= 3) {
  20. EmployeeVO employee = new EmployeeVO(1,"Lokesh","Gupta","howtodoinjava@gmail.com");
  21. return new ResponseEntity<EmployeeVO>(employee, HttpStatus.OK);
  22. }
  23. return new ResponseEntity(HttpStatus.NOT_FOUND);
  24. }
  25. }

2. Spring REST JSON – MappingJackson2JsonView支持

这是第二技术MappingJackson2JsonView类还取决于类路径中 Jackson JSON 处理器库的存在,因此您无需添加任何其他内容。 完整的pom.xml看起来像这样。

2.1. Maven 依赖

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.howtodoinjava.demo</groupId>
  5. <artifactId>springrestexample</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>springrestexample Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <dependencies>
  11. <dependency>
  12. <groupId>junit</groupId>
  13. <artifactId>junit</artifactId>
  14. <version>4.12</version>
  15. <scope>test</scope>
  16. </dependency>
  17. <!-- Spring MVC support -->
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-webmvc</artifactId>
  21. <version>4.1.4.RELEASE</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-web</artifactId>
  26. <version>4.1.4.RELEASE</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-core</artifactId>
  31. <version>4.1.4.RELEASE</version>
  32. </dependency>
  33. <!-- Jackson JSON Processor -->
  34. <dependency>
  35. <groupId>com.fasterxml.jackson.core</groupId>
  36. <artifactId>jackson-databind</artifactId>
  37. <version>2.4.1</version>
  38. </dependency>
  39. </dependencies>
  40. <build>
  41. <finalName>springrestexample</finalName>
  42. </build>
  43. </project>

2.2. 添加MappingJackson2JsonView视图

使用MappingJackson2JsonView类时,您将需要返回MappingJackson2JsonView类型的视图名称。 因此,您将需要更改两个位置。

2.2.1. 控制器变更

您将需要从控制器方法返回视图名称。在我们的例子中,视图名称是jsonTemplate

  1. package com.howtodoinjava.demo.controller;
  2. import org.springframework.http.HttpStatus;
  3. import org.springframework.http.MediaType;
  4. import org.springframework.http.ResponseEntity;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import com.howtodoinjava.demo.model.EmployeeListVO;
  10. import com.howtodoinjava.demo.model.EmployeeVO;
  11. @Controller
  12. public class EmployeeRESTController
  13. {
  14. private EmployeeListVO getEmployeesCollection()
  15. {
  16. EmployeeListVO employees = new EmployeeListVO();
  17. EmployeeVO empOne = new EmployeeVO(1,"Lokesh","Gupta","howtodoinjava@gmail.com");
  18. EmployeeVO empTwo = new EmployeeVO(2,"Amit","Singhal","asinghal@yahoo.com");
  19. EmployeeVO empThree = new EmployeeVO(3,"Kirti","Mishra","kmishra@gmail.com");
  20. employees.getEmployees().add(empOne);
  21. employees.getEmployees().add(empTwo);
  22. employees.getEmployees().add(empThree);
  23. return employees;
  24. }
  25. @RequestMapping(value = "/employees")
  26. public String getAllEmployeesJSON(Model model)
  27. {
  28. model.addAttribute("employees", getEmployeesCollection());
  29. return "jsonTemplate";
  30. }
  31. }

2.2.2. 配置变更

您将需要将视图名称jsonTemplate配置为类型为MappingJackson2JsonView的 bean。 并且您将需要配置类型为BeanNameViewResolver的视图解析器。 这样,视图名称jsonTemplate将与MappingJackson2JsonView匹配,并且将已解析的 JSON 响应返回给客户端。

RESTConfiguration.java

  1. @Configuration
  2. public class RESTConfiguration
  3. {
  4. @Bean
  5. public View jsonTemplate() {
  6. MappingJackson2JsonView view = new MappingJackson2JsonView();
  7. view.setPrettyPrint(true);
  8. return view;
  9. }
  10. @Bean
  11. public ViewResolver viewResolver() {
  12. return new BeanNameViewResolver();
  13. }
  14. }

与上述 java 配置等效的 XML 配置如下。

spring-servlet.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  5. http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd
  6. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  7. <context:component-scan base-package="com.howtodoinjava.demo" />
  8. <mvc:annotation-driven />
  9. <!-- JSON Support -->
  10. <bean name="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
  11. <bean name="jsonTemplate" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
  12. </beans>

3. Spring REST JSON 示例 – 演示

现在,当您点击 URL:http://localhost:8080/springrestexample/employees时,您将得到此结果。

Spring REST JSON 响应示例 - 图1

Spring REST JSON 示例

4. Spring REST JSON 示例 – 项目结构

Spring REST JSON 响应示例 - 图2

Spring REST JSON 示例 – 项目结构

这就是带有 spring mvc 的 spring restful web 服务 json 简单示例的全部内容。 将您的问题留在我的评论中。

学习愉快!