原文: http://zetcode.com/springboot/jersey/

Spring Boot Jersey 教程展示了如何在 Spring Boot 应用中使用 Jersey 建立一个简单的 RESTFul 应用。 Jersey 是使用@RestController创建的 Spring RESTFul 应用的替代方案。

Spring 是用于创建企业应用的流行 Java 应用框架。 Spring Boot 是 Spring 框架发展的下一步。 它有助于以最小的努力创建独立的,基于生产级的 Spring 应用。 它提倡在 XML 配置上使用约定而不是配置原则。

RESTFul 应用

RESTFul 应用遵循 REST 架构样式,该样式用于设计网络应用。 RESTful 应用生成对资源执行 CRUD(创建/读取/更新/删除)操作的 HTTP 请求。 RESTFul 应用通常以 JSON 或 XML 格式返回数据。

JAX-RS

RESTful Web 服务的 Java API(JAX-RS)是 Java 编程语言 API 规范,它提供了根据代表性状态传输(REST)架构模式创建 Web 服务的支持。 JAX-RS 使用注解来简化 Web 服务客户端和端点的开发和部署。 JAX-RS 是 Java EE 的正式组成部分。

Jersey

Jersey 是用于用 Java 开发 RESTful Web 服务的开源框架。 它是 RESTful Web 服务 Java API(JAX-RS)规范的参考实现。

Spring Boot Jersey 示例

以下应用是使用 Jersey 创建的 simpe Spring Boot RESTful 应用。

  1. $ tree
  2. .
  3. ├── pom.xml
  4. └── src
  5. ├── main
  6. ├── java
  7. └── com
  8. └── zetcode
  9. ├── Application.java
  10. ├── config
  11. └── JerseyConfig.java
  12. └── endpoint
  13. ├── HelloEndpoint.java
  14. └── ReverseReturnEndpoint.java
  15. └── resources
  16. └── test
  17. └── java
  18. └── com
  19. └── zetcode
  20. └── ApplicationTests.java

这是项目结构。

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.zetcode</groupId>
  8. <artifactId>SpringBootJersey</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. <packaging>jar</packaging>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <maven.compiler.source>1.8</maven.compiler.source>
  14. <maven.compiler.target>1.8</maven.compiler.target>
  15. </properties>
  16. <parent>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-parent</artifactId>
  19. <version>1.5.9.RELEASE</version>
  20. </parent>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-jersey</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-test</artifactId>
  29. <scope>test</scope>
  30. </dependency>
  31. </dependencies>
  32. <build>
  33. <plugins>
  34. <plugin>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-maven-plugin</artifactId>
  37. </plugin>
  38. </plugins>
  39. </build>
  40. </project>

这是 Maven 构建文件。 Spring Boot 启动器是一组方便的依赖项描述符,可以极大地简化 Maven 配置。 spring-boot-starter-parent具有 Spring Boot 应用的一些常用配置。 spring-boot-starter-jersey是使用 JAX-RS 和 Jersey 构建 RESTful Web 应用的入门。 它是spring-boot-starter-web的替代方法。 spring-boot-starter-test是使用包含 JUnit,Hamcrest 和 Mockito 的库测试 Spring Boot 应用的入门程序。

spring-boot-maven-plugin在 Maven 中提供了 Spring Boot 支持,使我们可以打包可执行的 JAR 或 WAR 档案。 它的spring-boot:run目标运行 Spring Boot 应用。

application.yml

  1. server:
  2. port: 8086
  3. context-path: /api
  4. spring:
  5. main:
  6. banner-mode: "off"
  7. logging:
  8. level:
  9. org:
  10. springframework: ERROR

application.yml文件中,我们编写了 Spring Boot 应用的各种配置设置。 我们设置端口和上下文路径。 使用banner-mode属性,我们可以关闭 Spring 横幅。

我们将 spring 框架的日志记录级别设置为ERROR。 在application.yml文件位于中src/main/resources目录。

JerseyConfig.java

  1. package com.zetcode.config;
  2. import com.zetcode.endpoint.HelloService;
  3. import com.zetcode.endpoint.ReverseService;
  4. import org.glassfish.jersey.server.ResourceConfig;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration
  7. public class JerseyConfig extends ResourceConfig {
  8. public JerseyConfig() {
  9. register(HelloService.class);
  10. register(ReverseService.class);
  11. }
  12. }

JerseyConfig注册两个服务类别。

HelloService.java

  1. package com.zetcode.service;
  2. import javax.ws.rs.GET;
  3. import javax.ws.rs.Path;
  4. import javax.ws.rs.Produces;
  5. import org.springframework.stereotype.Service;
  6. @Service
  7. @Path("/hello")
  8. public class HelloService {
  9. @GET
  10. @Produces("text/plain")
  11. public String hello() {
  12. return "Hello from Spring";
  13. }
  14. }

这是HelloService@Path注解定义了服务类将响应的 URL。 Spring 的@Service也注解了HelloService以进行自动检测。 我们的服务方法仅返回"Hello from Spring"消息。

HelloService.java

  1. package com.zetcode.service;
  2. import javax.validation.constraints.NotNull;
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.QueryParam;
  7. import org.springframework.stereotype.Service;
  8. @Service
  9. @Path("/reverse")
  10. public class ReverseService {
  11. @GET
  12. @Produces("text/plain")
  13. public String reverse(@QueryParam("data") @NotNull String data) {
  14. return new StringBuilder(data).reverse().toString();
  15. }
  16. }

reverse()服务方法返回一个反向的字符串。 它接受一个参数,不能为 null。 @QueryParam将 HTTP 查询参数的值绑定到资源方法参数。

ApplicationTests.java

  1. package com.zetcode;
  2. import static org.assertj.core.api.Assertions.assertThat;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
  8. import org.springframework.boot.test.web.client.TestRestTemplate;
  9. import org.springframework.http.HttpStatus;
  10. import org.springframework.http.ResponseEntity;
  11. import org.springframework.test.context.junit4.SpringRunner;
  12. @RunWith(SpringRunner.class)
  13. @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
  14. public class ApplicationTests {
  15. @Autowired
  16. private TestRestTemplate restTemplate;
  17. @Test
  18. public void hello() {
  19. ResponseEntity<String> entity = this.restTemplate.getForEntity("/hello",
  20. String.class);
  21. assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
  22. assertThat(entity.getBody()).isEqualTo("Hello from Spring");
  23. }
  24. @Test
  25. public void reverse() {
  26. ResponseEntity<String> entity = this.restTemplate
  27. .getForEntity("/reverse?data=regit", String.class);
  28. assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
  29. assertThat(entity.getBody()).isEqualTo("tiger");
  30. }
  31. @Test
  32. public void validation() {
  33. ResponseEntity<String> entity = this.restTemplate.getForEntity("/reverse",
  34. String.class);
  35. assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
  36. }
  37. }

ApplicationTests中,我们测试了两个端点。

Application.java

  1. package com.zetcode;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class Application {
  6. public static void main(String[] args) {
  7. SpringApplication.run(Application.class, args);
  8. }
  9. }

Application设置 Spring Boot 应用。 @SpringBootApplication启用自动配置和组件扫描。

  1. $ mvn spring-boot:run

使用mvn spring-boot:run命令,运行应用。 该应用部署在嵌入式 Tomcat 服务器上。

  1. $ curl localhost:8086/api/hello
  2. Hello from Spring

使用curl命令,我们连接到 hello 端点。

  1. $ curl localhost:8086/api/reverse?data=summer
  2. remmus

夏天的字符是相反的。

在本教程中,我们使用 Jersey 借助 Spring Boot 创建了一个简单的 RESTFul 应用,它是 JAX-RS 规范的参考实现。 您可能也对相关教程感兴趣: