JAX-RS:多路径

发布于 2024-10-13 15:25:33 字数 285 浏览 4 评论 0原文

可以做这样的事情吗?

import javax.ws.rs.GET;
import javax.ws.rs.Path;

public class xxx
{
  @GET
  @Path(value = "path1")
  public Response m1()
  {
    ...
  }

  @GET
  @Path(value = "path2")
  public Response m1()
  {
    ...
  }
}

顺便说一句,我正在使用 RESTEasy。

Is it possible to do something like that?

import javax.ws.rs.GET;
import javax.ws.rs.Path;

public class xxx
{
  @GET
  @Path(value = "path1")
  public Response m1()
  {
    ...
  }

  @GET
  @Path(value = "path2")
  public Response m1()
  {
    ...
  }
}

I'm using RESTEasy btw.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼泪也成诗 2024-10-20 15:25:33

是的,你可以这样做,尽管你必须重命名你的方法,以便它们的签名不同。

更新: 检查 Dieter Cailliau 的答案, @Path("/{a:path1|path2}") 可能就是你想要的...

public class BlahResource{
    @GET
    @Path("path1")
    public Response m1(){
        return Response.ok("blah").build();
    }

    @GET
    @Path("path2")
    public Response m2(){
        return this.m1();
}

你可以检查 JSR-311 的 API 及其名为“jersey”的参考实现:

JSR311 API

泽西岛

yes you can do that although you will have to rename your methods so that their signature is different.

Update: Check Dieter Cailliau's answer, @Path("/{a:path1|path2}") is probably what you want...

public class BlahResource{
    @GET
    @Path("path1")
    public Response m1(){
        return Response.ok("blah").build();
    }

    @GET
    @Path("path2")
    public Response m2(){
        return this.m1();
}

you can check JSR-311's API and it's reference implementation named "jersey" there:

JSR311 API

Jersey

ζ澈沫 2024-10-20 15:25:33

关于路径注释的一些额外细节...

正如之前的响应所述,在注释路径声明映射中使用的正则表达式:

{" variable-name [ ":" regular-expression ] "} 

您可以声明多个路径,但还有一个对我来说并不立即明显的路径层次结构,其中类注释路径作为以下方法路径注释的前缀。人们可以编写以下类来获得简洁的多路径选项,这对于资源版本控制可能很有用。

@Path("/{a:v1|v2}")
@Produces("text/*")
public class BlahResource {

    @GET
    @Path("/blah")
    public Response m1() {
        return Response.ok("blah").build();
    }
}

请注意,“BlahResource”类已使用路径“/v1”或“/v2”声明,使得资源可以通过以下方式访问

$ curl localhost:8080/v1/blah
blah

$ curl localhost:8080/v2/blah
blah

Some extra details about Path annotation...

As a previous responses state, regular expressions to be used with in the annotated path declaration mapping:

{" variable-name [ ":" regular-expression ] "} 

You can declare multiple paths, but there is also a path hierarchy that was not immediately obvious to me whereby the class annotated path prefixes the following method path annotations. One might write the following class for a concise multiple path option which could be useful for resource versioning perhaps.

@Path("/{a:v1|v2}")
@Produces("text/*")
public class BlahResource {

    @GET
    @Path("/blah")
    public Response m1() {
        return Response.ok("blah").build();
    }
}

Please note the fact that the class "BlahResource" has been declared with the path "/v1" or "/v2" making the resource accessible as:

$ curl localhost:8080/v1/blah
blah

and also

$ curl localhost:8080/v2/blah
blah
灼疼热情 2024-10-20 15:25:33

您可以使用子资源将两个路径映射到同一资源:

public class MySubResource {
    @GET
    public Response m1() {
        return Response.ok("blah").build();
    }
}

@Path("/root")
public class MyRootResource {

    @Path("/path1")
    public MySubResource path1() {
        return new MySubResource();
    }

    @Path("/path2")
    public MySubResource path2() {
        return new MySubResource();
    }
 }

You could use sub resources to map two paths to the same resource:

public class MySubResource {
    @GET
    public Response m1() {
        return Response.ok("blah").build();
    }
}

@Path("/root")
public class MyRootResource {

    @Path("/path1")
    public MySubResource path1() {
        return new MySubResource();
    }

    @Path("/path2")
    public MySubResource path2() {
        return new MySubResource();
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文