You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vert.x集成RESTEasy时@ApplicationPath注解未生效 接口返回404

问题根因

RESTEasy 与 Vert.x 集成时@ApplicationPath配置的路径前缀未生效,直接原因有两点:

  • JAX-RS 规范要求根级路径匹配的@ApplicationPath、@Path注解值必须以/开头,当前配置的@ApplicationPath("v1")属于相对路径,在非Servlet环境的Vert.x中无法被识别为全局路由前缀
  • resteasy-vertx 6.x 版本的VertxRequestHandler默认不会自动扫描@ApplicationPath注解提取路由前缀,需要手动给部署对象指定前缀,否则所有资源接口会直接注册到服务根路径,就会出现无/v1前缀的/health可访问、带前缀路径返回404的现象。
修复步骤

1. 修正Application类的路径注解

修改V1.java中的@ApplicationPath配置,给路径值添加开头的斜杠:

// 原注解 @ApplicationPath("v1")
@ApplicationPath("/v1")
public class V1 extends Application {
    private final Set<Object> singletons = new HashSet<>();

    public V1() {
        singletons.add(new Management());
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }
}

2. 给RESTEasy部署显式配置路由前缀

修改Main.java的初始化逻辑,在deployment启动前设置和注解一致的前缀:

public class Main extends AbstractVerticle {
    private static VertxResteasyDeployment deployment;

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();

        deployment = new VertxResteasyDeployment();
        deployment.setApplicationClass("starter.rx.endpoint.V1");
        // 新增该行,显式指定应用的路由前缀
        deployment.setPrefix("/v1");
        deployment.start();
        vertx.deployVerticle("starter.rx.Main");
    }

    @Override
    public Completable rxStart() {
        VertxRequestHandler handler = new VertxRequestHandler(vertx.getDelegate(), deployment);

        return vertx.createHttpServer()
                .requestHandler(x -> handler.handle(x.getDelegate()))
                .rxListen(80)
                .ignoreElement();
    }
}

可选优化

为了避免路径拼接时出现漏斜杠、双斜杠的异常,建议给Management.java中方法级的@Path也加上开头的斜杠,类上的空@Path("")可以直接保留:

@Path("")
public class Management {
    @GET
    // 原写法 @Path("health")
    @Path("/health")
    public CompletionStage<String> health() {
        return Flowable.just("healthy").singleStage("");
    }
}
验证结果

修改完成后重启服务,请求测试结果符合预期:

$ curl -i http://localhost:80/v1/health
HTTP/1.1 200 OK
content-length: 7

healthy

$ curl -i http://localhost:80/health
HTTP/1.1 404 Not Found
transfer-encoding: chunked

内容的提问来源于stack exchange,提问作者user7448575

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 20:42:21