SpringBoot——集成Jersey

本文详细讲解了在SpringBoot中集成Jersey的配置、WADL文档生成,并解决了Actuator与Jersey根路径冲突问题,提供了端口配置建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Jersey

1、SpringBoot

依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>

注意:jersety starter模式使用jackson解析JSON

配置

@Component
@ApplicationPath("/ws")
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        //注册单个资源类
        register(DemoResource.class);
        //或直接声明资源包所在位置
        // packages("pers.zhang.resource");
    }


}

资源类

@Path("hi")
public class DemoResource {

    @GET
    @Produces("application/json")
    public List<String> hi() {
        List<String> result = new ArrayList<>();
        result.add("hello world");
        result.add("hello Jersey");
        return result;
    }
}

测试

@Test
public void testGet() {
    Client client = ClientBuilder.newClient();
    WebTarget webTarget = client.target("http://127.0.0.1:8080/ws/hi");
    Response response = webTarget.request().get();
    System.out.println(response.readEntity(String.class));
}

输出:

["hello world","hello Jersey"]

wadl文档

访问jersey.application-path/application.wadl可以获取自动生成的wadl文档:

在这里插入图片描述

2、Actuator

依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

解决根路径冲突

在Jersey中引入Spring MVC会带来根路径冲突的问题,因为它们各自的Servlet都默认处理根路径。有如下几种解决方案:

使用同一个端口,分别指定各自的根路径:

server:
  port: 8080
# 启用所有监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
spring:
  jersey:
    application-path: /rest

mvc的根路径为/,jersey的跟路径为/rest
注意,如果同时也使用了@ApplicationPath("/ws")声明根路径,那么yml中的配置会覆盖注解中的配置。

使用不同的端口:

server:
  port: 8080
# 启用所有监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
  server:
    port: 8081

actuator的端口为8081,jersey的端口为8080

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值