简体   繁体   English

Jersey @Path 注释在类级别是强制性的

[英]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有什么可能的解决方法,使此类被识别为休息资源

Resource classes资源类

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

One possible solution一种可能的解决方案

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() {
        ...
    }
}

A better solution更好的解决方案

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.

相关问题 正则表达式在Jersey路径注释中 - Regex in Jersey Path Annotation 覆盖方法上的类级别@Path注释 - Override class level @Path annotation on the method @JsonProperty 是否有类级别的注释 - Is there a class level annotation for @JsonProperty Jersey路径注释未映射所有文件类型 - Jersey Path annotation not mapping all file types 这个 Spring Boot controller 方法注释有什么问题? (请求的路径在 class 级别定义,路径变量在方法中定义) - What is wrong in this Spring Boot controller method annotation? (the path of the request is defined at class level and the path variable at method) 在Jersey中使用JaxRS @Path注释匹配最后一个路径元素 - Match last path element using JaxRS @Path annotation in Jersey 有没有一种方法可以使特定类的子类中的特定Java注释成为必需的 - Is there a way to make a specific Java annotation mandatory in subclasses of a specific class Jersey Jetty Embedded无法读取路径注释,错误404 - Jersey Jetty Embedded Won't Read Path Annotation, Error 404 Jersey是否支持美元符号JAX-RS的Path注释? - Does Jersey support dollar sign in Path annotation of JAX-RS? 为什么错误的路径注释不会使Jersey崩溃REST API? - Why wrong path annotation doesn't crash REST API with Jersey?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM