Spring Boot Jersey
最后修改于 2023 年 8 月 2 日
Spring Boot Jersey 教程展示了如何在 Spring Boot 应用程序中使用 Jersey 设置一个简单的 RESTFul 应用程序。 Jersey 是使用 @RestController 创建的 Spring RESTFul 应用程序的替代方案。
Spring 是一个流行的 Java 应用程序框架,用于创建企业应用程序。 Spring Boot 是 Spring 框架演进的下一步。 它有助于以最小的努力创建基于 Spring 的独立、生产级应用程序。 它提倡使用约定优于配置原则,而不是 XML 配置。
RESTful 应用程序
RESTFul 应用程序遵循 REST 架构风格,用于设计网络应用程序。 RESTful 应用程序生成 HTTP 请求,对资源执行 CRUD(创建/读取/更新/删除)操作。 RESTFul 应用程序通常以 JSON 或 XML 格式返回数据。
JAX-RS
Java API for RESTful Web Services (JAX-RS) 是一个 Java 编程语言 API 规范,提供对根据表述性状态转移 (REST) 架构模式创建 Web 服务的支持。 JAX-RS 使用注解来简化 Web 服务客户端和端点的开发和部署。 JAX-RS 是 Java EE 的官方组成部分。
Jersey
Jersey 是一个用于在 Java 中开发 RESTful Web 服务的开源框架。 它是 Java API for RESTful Web Services (JAX-RS) 规范的参考实现。
Spring Boot Jersey 示例
以下应用程序是一个使用 Jersey 创建的简单 Spring Boot RESTful 应用程序。
build.gradle
...
src
├── main
│ ├── java
│ │ └── com
│ │ └── zetcode
│ │ ├── Application.java
│ │ ├── config
│ │ │ └── JerseyConfig.java
│ │ └── endpoint
│ │ ├── HelloEndpoint.java
│ │ └── ReverseReturnEndpoint.java
│ └── resources
└── test
└── java
└── com
└── zetcode
└── ApplicationTests.java
这是项目结构。
plugins {
id 'org.springframework.boot' version '3.1.1'
id 'io.spring.dependency-management' version '1.1.0'
id 'java'
}
group = 'com.zetcode'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-jersey'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
这是 Gradle 构建文件。 spring-boot-starter-jersey 是一个用于构建 RESTful Web 应用程序的启动器,使用 JAX-RS 和 Jersey。 它是 spring-boot-starter-web 的替代方案。
spring-boot-starter-test 是一个用于使用包括 JUnit、Hamcrest 和 Mockito 等库来测试 Spring Boot 应用程序的启动器。
server:
port: 8086
context-path: /api
spring:
main:
banner-mode: "off"
logging:
level:
org:
springframework: ERROR
在 application.yml 文件中,我们编写了 Spring Boot 应用程序的各种配置设置。 我们设置了端口和上下文路径。 通过 banner-mode 属性,我们关闭了 Spring banner。
我们将 spring 框架的日志级别设置为 ERROR。 application.yml 文件位于 src/main/resources 目录中。
package com.zetcode.config;
import com.zetcode.service.HelloService;
import com.zetcode.service.ReverseService;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(HelloService.class);
register(ReverseService.class);
}
}
JerseyConfig 注册两个服务类。
package com.zetcode.service;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import org.springframework.stereotype.Service;
@Service
@Path("/hello")
public class HelloService {
@GET
@Produces("text/plain")
public String hello() {
return "Hello from Spring";
}
}
这是 HelloService。 @Path 注解定义了服务类将响应的 URL。 HelloService 也使用 Spring 的 @Service 进行自动检测。 我们的服务方法只是返回“Hello from Spring”消息。
package com.zetcode.service;
import javax.validation.constraints.NotNull;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import org.springframework.stereotype.Service;
@Service
@Path("/reverse")
public class ReverseService {
@GET
@Produces("text/plain")
public String reverse(@QueryParam("data") @NotNull String data) {
return new StringBuilder(data).reverse().toString();
}
}
reverse 服务方法返回一个被反转的字符串。 它接受一个参数,该参数不能为空。 @QueryParam 将 HTTP 查询参数的值绑定到资源方法参数。
package com.zetcode;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ApplicationTests {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void hello() {
ResponseEntity<String> entity = this.restTemplate.getForEntity("/hello",
String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("Hello from Spring");
}
@Test
public void reverse() {
ResponseEntity<String> entity = this.restTemplate
.getForEntity("/reverse?data=regit", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).isEqualTo("tiger");
}
@Test
public void validation() {
ResponseEntity<String> entity = this.restTemplate.getForEntity("/reverse",
String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
}
}
在 ApplicationTests 中,我们测试了两个端点。
package com.zetcode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Application 设置了 Spring Boot 应用程序。@SpringBootApplication 启用了自动配置和组件扫描。
$ ./gradlew bootRun
我们运行应用程序。 应用程序部署在嵌入式 Tomcat 服务器上。
$ curl localhost:8086/api/hello Hello from Spring
使用 curl 命令,我们连接到 hello 端点。
$ curl localhost:8086/api/reverse?data=summer remmus
夏季的字符被反转。
在本文中,我们使用 Jersey 在 Spring Boot 中创建了一个简单的 RESTFul 应用程序,它是 JAX-RS 规范的参考实现。