Spring Boot Jersey 教程展示了如何在 Spring Boot 应用程序中使用 Jersey 设置简单的 RESTFul 应用程序。Jersey 是Spring Boot @RestController.的替代。
Spring是一种流行的 Java 应用程序框架,用于创建企业应用程序。 Spring Boot是 Spring 框架演变的下一步。它有助于以最小的努力创建独立的、生产级的基于 Spring 的应用程序。它提倡使用约定优于配置原则而不是 XML 配置。
RESTful 应用程序
RESTFul 应用程序遵循 REST 架构风格,用于设计网络应用程序。RESTful 应用程序生成对资源执行 CRUD(创建/读取/更新/删除)操作的 HTTP 请求。RESTFul 应用程序通常以 JSON 或 XML 格式返回数据。
JAX-RS
Java API for RESTful Web Services (JAX-RS)是一种 Java 编程语言 API 规范,它支持根据 Representational State Transfer (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 应用程序。

这是项目结构。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringBootJersey</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootJersey</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<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 starters 是一组方便的依赖描述符,极大地简化了 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 应用程序的入门工具。
在 Maven中spring-boot-maven-plugin提供 Spring Boot 支持,允许我们打包可执行的 JAR 或 WAR 档案。它的spring-boot:run目标是运行 Spring Boot 应用程序。
server.port=8086
server.servlet.context-path= /api
spring.main.banner-mode= "off"
logging.level.org.springframework= ERROR
在application.properties文件中,我们编写了 Spring Boot 应用程序的各种配置设置。我们设置端口和上下文路径。使用该banner-mode属性,我们关闭了 Spring 横幅。
我们将 spring 框架的日志级别设置为 ERROR。application.properties文件位于src/main/resources目录中。
package com.example.SpringBootJersey.config;
import com.example.SpringBootJersey.service.HelloService;
import com.example.SpringBootJersey.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.example.SpringBootJersey.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 。HelloService还用 Spring 的@Service自动检测注释。我们的服务方法只返回“Hello from Spring”消息。
package com.example.SpringBootJersey.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()service 方法返回一个反转的字符串 。它接受一个参数,不能为空。@QueryParam 将 HTTP 查询参数的值绑定到资源方法参数。
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,我们测试了两个端点。
package com.example.SpringBootJersey;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootJerseyApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootJerseyApplication.class, args);
}
}
Application设置 Spring Boot 应用程序 。@SpringBootApplication启用自动配置和组件扫描 。
$ mvn spring-boot:run
使用mvn spring-boot:run命令,我们运行应用程序。该应用程序部署在嵌入式 Tomcat 服务器上。
$ curl localhost:8086/api/hello
Hello from Spring
使用该curl命令,我们连接到 hello 端点。
$ curl localhost:8086/api/reverse?data=summer
remmus
夏天的角色被颠倒了。
在本教程中,我们在 Spring Boot 中使用 Jersey 创建了一个简单的 RESTFul 应用程序,它是 JAX-RS 规范的参考实现。
本文档介绍了如何在SpringBoot应用程序中使用Jersey创建RESTful服务。Jersey作为JAX-RS规范的参考实现,提供了一种替代SpringBoot@RestController的方法。示例包括创建HelloService和ReverseService,以及相应的单元测试。通过配置文件、服务类和测试,展示了如何设置和运行这样的应用。
1723

被折叠的 条评论
为什么被折叠?



