Spring Boot 集成Jersey 创建JAX-RS RESTFul 应用程序。

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

Spring Boot 集成Jersey 创建JAX-RS RESTFul 应用程序。_第1张图片

这是项目结构。

 

pom.xml


	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.6.4
		 
	
	com.example
	SpringBootJersey
	0.0.1-SNAPSHOT
	SpringBootJersey
	Demo project for Spring Boot
	
		1.8
	
	
		
			org.springframework.boot
			spring-boot-starter-jersey
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


这是 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 应用程序。

application.properties
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目录中。

JerseyConfig.java
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注册两个服务类。

HelloService.java
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”消息。

ReverseService.java
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 查询参数的值绑定到资源方法参数。

ApplicationTests.java
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 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 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 entity = this.restTemplate.getForEntity("/reverse",
                String.class);
        assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
    }
}

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

SpringBootJerseyApplication.java
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 规范的参考实现。

你可能感兴趣的:(java,json)