TedTrippin
TedTrippin

Reputation: 3662

Jersey Getting a HTTP 405, path mapping not handled as expected

I have the following 2 methods...

@GET
@Path("/{solution}")
public Response test(@PathParam("solution") String solution,
        @Context UriInfo uriInfo, @Context HttpHeaders headers);

@GET
@Path("/{solution}/{path:[a-z0-9/\\-]*}")
public Response testTest(@PathParam("solution") String solution,
        @PathParam("path") String nodePath,
        @Context UriInfo uriInfo, @Context HttpHeaders headers);

When I call /my-app/test/test the second method gets called and the parameters set correctly. When I call /my-app/test instead of the first method getting called I receive a HTTP 405 Method Not Found. I'm assuming it's mapping the URL to one of the other methods eg.

@DELETE
@Path("{path: [a-z0-9/\\-]*}")
public Response deleteTest(@PathParam("path") String path, @Context HttpHeaders headers);

Anyone spot what I've done wrong? Anyone got any tips for finding out which method jersey is trying to map to?

Thanks.

Upvotes: 1

Views: 2661

Answers (1)

Martin Matula
Martin Matula

Reputation: 7989

With the 3 methods defined as above you essentially have 3 resources:

  • /{solution}
  • /{solution}/{path:[a-z0-9/\-]*}
  • {path: [a-z0-9/\-]*}

When Jersey matches the request to resource methods, it first determines which resource (i.e. URI pattern) it should be mapped to. The third one is the most specific one that matches the request URL, so that one is matched. Only then Jersey looks if a corresponding HTTP method is handled for that resource. This is in compliance with the JAX-RS specification - you can see the detailed description of the matching algorithm in the spec here: http://jsr311.java.net/nonav/releases/1.1/spec/spec3.html#x3-360003.7.2

So, you should think about the logical resources, have the URI templates match them and then implement the corresponding HTTP methods for these. I.e. if "/{solution}" and "/{solution}/{path:[a-z0-9/\-]*}" are logically two distinct resources, and both should support @DELETE, you should define two delete operations, one with the first template and the other one with the second URI template.

Upvotes: 5

Related Questions