Just adding my two cents. The JAX-RS 2.0 specification (which Jersey implements) is very clear about how the request matching works:
3.7.2 Request Matching
A request is matched to the corresponding resource method or sub-resource method by comparing the normalized request URI, the media type of any request entity, and the requested response entity format to the metadata annotations on the resource classes and their methods. If no matching resource method or sub-resource method can be found then an appropriate error response is returned. [...]
Besides the NotFoundException
(404 status) generated when no matching resources (classes and methods) can be found, the appropriate error response include (quoting the JAX-RS 2.0 specification again):
If no methods support the request method an implementation MUST generate a NotAllowedException
(405 status) and no entity.
If no methods support the media type of the request entity body an implementation MUST generate a NotSupportedException
(415 status) and no entity.
If no methods support one of the acceptable response entity body media types an implementation MUST generate a NotAcceptableException
(406 status) and no entity.
Summarizing, you'll receive the appropriate error message when the request doesn't match your resources.
If you need, you can create a pre-matching filter, which is executed before the request matching is started. This filter allows you to modify the request. You can easily change the requested method, the requested URI and the requested headers.
To do it, just implement ContainerRequestFilter
and annotate it with @PreMatching
:
@Provider
@PreMatching
public class PreMatchingFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
// Use the requestContext to modify the request
}
}
For more details, have a look at the ContainerRequestContext
API.