Vineet Singla
Vineet Singla

Reputation: 1677

@Path configuration at class level and method level

I am using javax.ws.rs.Path class in my REST class to configure path. It is behaving strange ... My configuration is something like this... This is not actual files, but I have shown a replication to make you understand my configurations.

Class1

@Path("/v2")
public class BoxResource {        
   @POST
   @Path("/ie/box")
   public Response createbox(...) {
     ...
   }
}

Class2

@Path("/v2/ie")
public class BagResource {        
    @POST
    @Path("/bag")
    public Response createbag(...) {
        ...
    }        
}

When I make a HTTP request like /v2/ie/box, it throws the Server configuration error. But if I change my Class2 like below , it works fine.

@Path("/v2/ie/bag")
public class BagResource {            
    @POST
    public Response createbag(...) {

    }
}

Why is it like .. Does configuration of @path at class level and method level differs?

Upvotes: 10

Views: 8276

Answers (1)

user1907906
user1907906

Reputation:

A request to /v2/ie/box is mapped to Class2 because is has

@Path("/v2/ie")

as a class annotation.

The longest Path wins in this case. Class1 is never looked at since it has the shorter class annotation @Path("/v2").

Since Class2 has no method that maps to the remaining /box, you will get a 404 Not Found.

Recommendation

  1. Use @Path annotations only on the methods: @Path("/v2/ie/bag") and @Path("/v2/ie/box") OR
  2. use one resource class with a class annotation of @Path("/v2/ie") and method annotations of @Path("/bag") and @Path("/box").

Edit

See also section "3.7 Matching Requests to Resource Methods" of the JAX-RS 2.0 spec.

Upvotes: 16

Related Questions