Overriding @Path at Jersey

…衆ロ難τιáo~ 提交于 2019-12-24 03:52:57

问题


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.

Example 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!


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/2681504/overriding-path-at-jersey

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!