[英]Jersey @Path annotation mandatory at class level
I have a class like:我有一个类:
public class TestService {
@Path("/v1/test1/list")
public Response getTest1() {
}
@Path("/v1/test2/list")
public Response getTest2() {
}
}
If I do not give @Path annotation at Class level, then this class is not recognized as a REST resource, but I cannot give "/v1" Path for this class since there is already another class with @Path("/v1").如果我不在类级别给出 @Path 注释,那么这个类就不会被识别为 REST 资源,但我不能为这个类提供“/v1”路径,因为已经有另一个带有 @Path("/v1") 的类.
What are possible workaround, to make this class to be recognized as a Rest Resource有什么可能的解决方法,使此类被识别为休息资源
A@Path
annotation is required to define a resource class .定义资源类需要
@Path
注释。 Quoting the 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
.根资源类是POJO(普通旧式Java对象)被标注有
@Path
,具有注释的与至少一种方法@Path
或资源方法指示符注释如@GET
,@PUT
,@POST
,@DELETE
。
As already mentioned by Justas , one possible solution is to add the @Path("")
annotation to the TestService
class.正如Justas已经提到的,一种可能的解决方案是将
@Path("")
注释添加到TestService
类。 However, it doesn't smell good:然而,它闻起来并不好:
@Path("")
public class TestService {
@GET
@Path("/v1/test1/list")
public Response getTest1() {
...
}
@GET
@Path("/v1/test2/list")
public Response getTest2() {
...
}
}
I don't know what your project looks like, but instead of having a single class, I would have two classes, designed as following:我不知道你的项目是什么样的,但我会有两个类,而不是一个单一的类,设计如下:
@Path("/v1/test1")
public class TestService1 {
@GET
@Path("/list")
public Response getTest1() {
...
}
}
@Path("/v1/test2")
public class TestService2 {
@GET
@Path("/list")
public Response getTest2() {
...
}
}
You can add empty path @Path("")
or @Path("/")
.您可以添加空路径
@Path("")
或@Path("/")
。 However, this problem may show that you should design your code differently.但是,此问题可能表明您应该以不同的方式设计代码。
The @Path
annotation is used to specify the URI
through which a resource and an API can be accessed. @Path
注释用于指定可以访问资源和 API 的URI
。 Resource in this case is the REST
Web service itself.在这种情况下,资源是
REST
Web 服务本身。 Thus this annotation is present at the class level as well as the method level.因此,此注释存在于类级别以及方法级别。 It is mandatory to annotate a
REST
Web resource class with the @Path
annotation.必须使用
@Path
注释来注释REST
Web 资源类。 Thus if a user wants to access the 'Countries' through the resource 'HelloWorld' context, then:因此,如果用户想通过资源“HelloWorld”上下文访问“Countries”,则:
Resource at the class level would have @Path("/")
.类级别的资源将具有
@Path("/")
。 This is the default annotation at the class level.这是类级别的默认注释。 Resource at the API level would have
@Path("Countries")
annotation. API 级别的资源将具有
@Path("Countries")
注释。 As a best practice, the class-level path annotation should be a noun that qualifies the Web service, and the method-level annotation can be a noun or an action (for example, user and add-user, respectively).作为最佳实践,类级别的路径注释应该是限定 Web 服务的名词,方法级别的注释可以是名词或操作(例如分别为 user 和 add-user)。
https://www.juniper.net/documentation/en_US/junos-space-sdk/13.1/apiref/com.juniper.junos_space.sdk.help/html/reference/spaceannoREST.html https://www.juniper.net/documentation/en_US/junos-space-sdk/13.1/apiref/com.juniper.junos_space.sdk.help/html/reference/spaceannoREST.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.