在Jersey覆盖@Path [英] Overriding @Path at Jersey

查看:125
本文介绍了在Jersey覆盖@Path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试制作一个从路径上采用以下结构的Jersey应用程序:

I've been trying to make a Jersey app that takes the following structure from the path:

示例1(由于堆栈溢出限制,省略了http://) example.com/source/{source-id}/ example.com/source/

Example 1 (http:// omitted due to stackoverflow restrictions) example.com/source/{source-id}/ example.com/source/

带代码(省略了错误处理和不需要的代码):

With code (error handling and unneeded code omitted):

带代码(加总):

@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();
 }

}

它工作正常.

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

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();

 }
}

在示例2中,我可以访问诸如example.com/data/111/222/这样的URL,但是尝试获取hexample.com/data/111/会给我一个405错误代码(不允许使用方法).我还尝试制作一个完整的方法来检查{data-id}是否为空或空白,并且在这种情况下,像第一种方法一样处理请愿书,但也不起作用.

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.

有什么主意吗?

谢谢!

推荐答案

我刚刚将您的示例2粘贴到了Resource中,它似乎可以正常工作.我正在使用Jersey 1.1.5.

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

下面是我的测试结果.

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

这篇关于在Jersey覆盖@Path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆