gpt4 book ai didi

java - Jersey @Path 注释在类级别是必需的

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:13 25 4
gpt4 key购买 nike

我有一个类:

public class TestService {

@Path("/v1/test1/list")
public Response getTest1() {
}

@Path("/v1/test2/list")
public Response getTest2() {
}

}

如果我不在类级别提供@Path 注释,则此类不会被识别为 REST 资源,但我无法为此类提供“/v1”路径,因为已经有另一个类带有 @Path("/v1").

有什么可能的解决方法,使此类被识别为 Rest 资源

最佳答案

资源类

A @Path需要注解来定义一个资源类。引用 Jersey documentation :

Root resource classes are POJOs (Plain Old Java Objects) that are annotated with @Path, have at least one method annotated with @Path or a resource method designator annotation such as @GET, @PUT, @POST, @DELETE.

一种可能的解决方案

已经mentioned by Justas ,一种可能的解决方案是将 @Path("") 注释添加到 TestService 类。然而,它闻起来并不好:

@Path("")
public class TestService {

@GET
@Path("/v1/test1/list")
public Response getTest1() {
...
}

@GET
@Path("/v1/test2/list")
public Response getTest2() {
...
}
}

更好的解决方案

我不知道你的项目是什么样的,但我不会有一个类,而是有两个类,设计如下:

@Path("/v1/test1")
public class TestService1 {

@GET
@Path("/list")
public Response getTest1() {
...
}
}
@Path("/v1/test2")
public class TestService2 {

@GET
@Path("/list")
public Response getTest2() {
...
}
}

关于java - Jersey @Path 注释在类级别是必需的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39238821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com