Reputation: 1677
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
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
@Path
annotations only on the methods: @Path("/v2/ie/bag")
and @Path("/v2/ie/box")
OR@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