我们可以为同一个REST方法使用多个@Path注释吗? [英] Can we have more than one @Path annotation for same REST method

查看:197
本文介绍了我们可以为同一个REST方法使用多个@Path注释吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们是否可以为同一个REST方法提供多个 @Path 注释,即执行的方法是相同的,但是在访问多个URL时执行?

Can we have more than one @Path annotation for same REST method i.e. the method executed is the same, but it is executed on accessing more than one URL?

例如:我想在 http:/上运行 searchNames()方法/ a / b / c http:// a / b

E.g.: I want to run the searchNames() method on both http://a/b/c and http://a/b.

推荐答案

您不能在单个方法上使用mutliple @Path 注释。它会导致重复注释语法错误。

You can't have mutliple @Path annotations on a single method. It causes a "duplicate annotation" syntax error.

但是,有多种方法可以有效地将两个路径映射到方法。

However, there's a number of ways you can effectively map two paths to a method.

@Path JAX-RS中的注释接受参数,其值可以使用正则表达式进行限制。

The @Path annotation in JAX-RS accepts parameters, whose values can be restricted using regular expressions.

此注释:

@Path(a / {parameter:path1 | path2})

将启用该方法请求 / a / path1 / a / path2 。如果需要使用子路径,则转义斜杠: {a:path1 \\ / subPath1 | path2 \\ / subPath2}

would enable the method to be reached by requests for both /a/path1 and /a/path2. If you need to work with subpaths, escape slashes: {a:path1\\/subPath1|path2\\/subPath2}

或者,您可以设置重定向。这是通过定义另一个子资源在Jersey(JAX-RS的参考实现)中实现它的方法。这只是一个例子,如果你喜欢不同的处理重定向的方式,请随意使用它。

Alternatively, you could set up a redirection. Here's a way to do it in Jersey (the reference implementation of JAX-RS), by defining another subresource. This is just an example, if you prefer a different way of handling redirections, feel free to use it.

@Path("basepath")
public class YourBaseResource {

  //this gets injected after the class is instantiated by Jersey    
  @Context
  UriInfo uriInfo; 

  @Path("a/b")
  @GET
  public Responce method1(){
    return Response.ok("blah blah").build();
  }

  @Path("a/b/c")
  @GET
  public Response method2(){
    UriBuilder addressBuilder = uriInfo.getBaseUriBuilder();
    addressBuilder.path("a/b");
    return Response.seeOther(addressBuilder.build()).build();
  }

}



使用servlet过滤器重写URL



如果您经常需要这样的功能,我建议使用servlet过滤器拦截传入的请求并动态重写路径。这应该可以帮助您将所有重定向保存在一个位置。理想情况下,您可以使用现成的库。只要您对BSD没问题, UrlRewriteFilter 就可以解决问题许可证(请查看他们的谷歌代码网站了解详情)

Using a servlet filter to rewrite URLs

If you're going to need such functionality often, I suggest intercepting the incoming requests using a servlet filter and rewriting the paths on the fly. This should help you keep all redirections in one place. Ideally, you could use a ready library. UrlRewriteFilter can do the trick, as long as you're fine with a BSD license (check out their google code site for details)

另一种选择是通过在Java应用程序前设置代理来处理此问题。您可以设置Apache服务器以提供基本的缓存和重写规则,而不会使Java代码复杂化。

Another option is to handle this with a proxy set up in front of your Java app. You can set up an Apache server to offer basic caching and rewrite rules without complicating your Java code.

这篇关于我们可以为同一个REST方法使用多个@Path注释吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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