Overriding @Path in Jersey

I am trying to make a Jersey application that takes the following structure from the path:

Example 1 (http: // skipped due to stack overlap constraints) example.com/source/ {source-id }/ example.com/source/

With code (error handling and unnecessary code omitted):

With code (summarizing):

@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 great.

Example 2: example.com/data/ {source-id }/ {info-id }/ example.com/data/ {source-id }/

With code (error handling and unnecessary 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 a URL like example.com/data/111/222/, but trying to get hexample.com/data/111/ gives me a 405 error code (method not allowed). I tried to create a whole method as well that checks if {data-id} is empty or empty, in which case it handles the petition as in the first method, but also doesn't work.

Any idea?

Thanks!

+2


a source to share


2 answers


I just put your example 2 into a resource and it seems to work fine. I am using Jersey 1.1.5.

Below are the results of 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

      

+1


a source


You can try using the path for the root resource for example @Path("/data/{source-id}")

. Then the first method will only have @GET

annotation and the second one will have @GET @Path("{data-id}")

.



Moreover, I would suggest using different method names (although they probably do different things), but I'm not sure if this is the cause of your problem.

0


a source







All Articles