I've been trying to make a Jersey app that takes the following structure from the path:
Example 1 (http:// omitted due to stackoverflow restrictions) example.com/source/{source-id}/ example.com/source/
With code (error handling and unneeded code omitted):
With code (sum up):
@Path("/source/")
public class SourceREST {
...
@GET
public Response getSource() {
return Response.ok("Sources List (no field)").build();
}
@GET
@Path("/{source-id}")
public Response getSource(@PathParam("source-id") String sourceID) {
return Response.ok("Source: " + sourceID).build();
}
}
It works fine.
Ex开发者_StackOverflowample 2: example.com/data/{source-id}/{info-id}/ example.com/data/{source-id}/
With code (error handling and unneeded code omitted):
@Path("/data/")
public class DataREST {
...
@GET
@Path("/{source-id}")
public Response getContext(@PathParam("source-id") String sourceID) {
return Response.ok("Source: " + sourceID + " Last ContextInfo").build();
}
@GET
@Path("/{source-id}/{data-id}")
public Response getContext(@PathParam("source-id") String sourceID,
@PathParam("data-id") String dataID) {
return Response.ok("Source: " + sourceID + " Data: " + dataID).build();
}
}
In example 2, I can access an URL like example.com/data/111/222/ fine, but trying to get hexample.com/data/111/ gives me a 405 Error Code (Method Not Allowed). I've tried also to make a whole method that checks if {data-id} is empty or blank and in that case process the petition as in the first method, but doesn't work either.
Any idea?
Thanks!
I just pasted your example 2 into a Resource and it seems to work fine. I am using Jersey 1.1.5.
Below are the results from my tests.
http://localhost:8090/MyHealth/helloworld/111
Source: 111 Last ContextInfo
http://localhost:8090/MyHealth/helloworld/111/
Source: 111 Last ContextInfo
http://localhost:8090/MyHealth/helloworld/111/222
Source: 111 Data: 222
http://localhost:8090/MyHealth/helloworld/111/222/
Source: 111 Data: 222
You can try using path for root resource like @Path("/data/{source-id}")
. Then the first method would only have @GET
annotation while the second one would have @GET @Path("{data-id}")
.
What is more, I would suggest using different method names (while they probably performs different actions), but I'm not sure that it is the cause of your problem.
精彩评论