简体   繁体   中英

Can RestEasy extends @Path?

@Path("a")
public class A {}

@Path("b")
public class B extends A {
    @GET
    getList(){}
}

I want a path to be GET localhost/rest/v1/a/b

Is it a way to do this? simple extending doesn't solve the problem

Annotations can be inherited , but they are not extended or concatenated or anything like that. Put it this way - the @Path on B completely erases the @Path from A . (Without B 's @Path , it would inherit the @Path from A , possibly leading to a deployment conflict, but that's neither here nor there.)

There are two ways, off the top of my head, to do this. The first, of course, simply involves doing this:

@Path("a/b")
public class B ...

The second involves sub-resources (section 3.4.1 of the JAX-RS 2.0 spec )...

@Path("a")
public class A {

    @Path("b")
    public B getB() {
       return new B();
    }
}

public class B {
    // blah blah blah
}

Two things here I want to highlight:

  1. B has no @Path of its own. It is a subresource, not directly accessible except through A . The total path to B is the concatenation of the application path, A 's path, and B s path on the subresource locator ( getB() ), with appropriate / s.

  2. B does not inherit from A . If it did, as I said above, it would inherit A 's @Path. This could produce a conflict, or lead to A being ignored in favor of the more specific subtype B . (See section 3.6 of the spec for details of annotation inheritance, and section 3.7 for exactly how a matching class/method is chosen for a given request.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM