对于同一个 REST 方法,我们可以有多个 @Path 注释吗 [英] Can we have more than one @Path annotation for same REST method

查看:36
本文介绍了对于同一个 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://a/b/chttp://a/b 上运行 searchNames() 方法.

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

推荐答案

你不能在一个方法上有多个 @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.

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

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 过滤器拦截传入的请求并即时重写路径.这应该可以帮助您将所有重定向保存在一个地方.理想情况下,您可以使用现成的库.UrlRewriteFilter 可以做到这一点,只要你有 BSD 许可证就行(查看他们的谷歌详细代码站点)

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天全站免登陆