Spring Boot Jersey 教程

来源:这里教程网 时间:2026-02-17 21:33:10 作者:

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 应用。

$ tree.├── pom.xml└── src    ├── main    │   ├── java    │   │   └── com    │   │       └── zetcode    │   │           ├── Application.java    │   │           ├── config    │   │           │   └── JerseyConfig.java    │   │           └── endpoint    │   │               ├── HelloEndpoint.java    │   │               └── ReverseReturnEndpoint.java    │   └── resources    └── test        └── java            └── com                └── zetcode                    └── ApplicationTests.java

这是项目结构。

pom.xml

<?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.zetcode</groupId>    <artifactId>SpringBootJersey</artifactId>    <version>1.0-SNAPSHOT</version>    <packaging>jar</packaging>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <maven.compiler.source>1.8</maven.compiler.source>        <maven.compiler.target>1.8</maven.compiler.target>    </properties>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.9.RELEASE</version>    </parent>       <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-jersey</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>              </dependencies>      <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>                    </plugins>    </build>        </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

server:    port: 8086    context-path: /apispring:     main:        banner-mode: "off"     logging:     level:         org:             springframework: ERROR

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

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

JerseyConfig.java

package com.zetcode.config;import com.zetcode.endpoint.HelloService;import com.zetcode.endpoint.ReverseService;import org.glassfish.jersey.server.ResourceConfig;import org.springframework.context.annotation.Configuration;@Configurationpublic class JerseyConfig extends ResourceConfig {    public JerseyConfig() {        register(HelloService.class);        register(ReverseService.class);    }}

JerseyConfig注册两个服务类别。

HelloService.java

package com.zetcode.service;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.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。 Spring 的@Service也注解了HelloService以进行自动检测。 我们的服务方法仅返回“ Hello from Spring”消息。

HelloService.java

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

ApplicationTests.java

package com.zetcode;import static org.assertj.core.api.Assertions.assertThat;import org.junit.Test;import org.junit.runner.RunWith;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 org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@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中,我们测试了两个端点。

Application.java

package com.zetcode;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

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

$ mvn spring-boot:run

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

$ curl localhost:8086/api/helloHello from Spring

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

$ curl localhost:8086/api/reverse?data=summerremmus

夏天的字符是相反的。

在本教程中,我们使用 Jersey 借助 Spring Boot 创建了一个简单的 RESTFul 应用,它是 JAX-RS 规范的参考实现。

相关推荐