JAX-RS - 两个类可以具有相同的 @Path 标识符吗?

发布于 2024-11-02 00:44:28 字数 511 浏览 10 评论 0原文

我有一个重定向到特定 URI 的 Web 应用程序:比方说 /service/library。在另一个包中,我有一个 jaxrs 服务器,用于侦听 URI 中的 /service,并定义一些 bean 来处理请求。那里已经有相当多的 Bean,并且已经实现了其中一个类来处理 /service/library 的请求。我正在尝试创建一个新类,它也处理 /service/library 的请求,但具有不同的绝对 URI 路径,例如:/service/library/mynewlibrary。我的问题是,是否可以在两个类中定义相同的 @Path 标识符,或者它们必须是唯一的,换句话说,我是否需要为我的新类使用像 /service/mylibrary 这样的 URI类实现而不是实现也使用相同 @Path 标识符的第二个类?我对 JAX-RS 还很陌生,所以我希望我的问题有意义!

谢谢!

I have a webapp that redirects to a particular URI: let's say /service/library. In another bundle, I have a jaxrs server that listens for /service in the URI, and defines some beans to handle the request. There are quite a few beans there already, and one of the classes is already implemented to handle requests for /service/library. I am trying to create a new class that also handles requests for /service/library, but with a different absolute URI path, for example: /service/library/mynewlibrary. My question is, is it possible to define the same @Path identifier in two classes, or must they be unique, in other words, will I need to use a URI like /service/mylibrary for my new class implementation instead of implementing a second class that also uses the same @Path identifier? I am pretty new to JAX-RS, so I hope my question makes sense!

Thanks!

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

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

发布评论

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

评论(3

说谎友 2024-11-09 00:44:28

可以有两个与 URI 匹配的 @Path 注释。在您的情况下,如果 servlet-mapping 是 service,您可能有 @Path("/library")@Path("library/mynewlibrary")< /代码>。
当请求到达时,匹配的路径按降序排序,因此当带有 /service/library/mynewlibrary 的请求到达时,应该调用第二个类。

It's possible to have two @Path annotations that match the URI. In your case, if servlet-mapping is service, you may have @Path("/library") and @Path("library/mynewlibrary").
When request arrives, the matching paths are sorted in descending order, so the second class should be called, when a request with /service/library/mynewlibrary arrives.

雨后咖啡店 2024-11-09 00:44:28

很可能有两个方法具有相同的 @Path 注释,例如,如果它们通过其他方式(例如 HTTP 方法或 @Consumes 注释)进行区分。类上的 @Path 充当类方法上的 @Path 的默认/根。此外,如果一条路径位于另一条路径“之内”,那根本不是问题。 JAX-RS 指定使用可能的最具体的匹配。 (我宁愿不这样做,而是让“外部”类在适当的部分匹配上返回对“内部”类的引用,以便每条路径都有一条可追踪的责任路线,肯定会导致单个类。不过,这需要一种相当不同的方式来排列 @Path 注释。)

但是,如果您最终得到了可以服务相同传入请求的两种方法,那么您就会遇到冲突,并且 JAX- RS 实现可以自由选择使用哪一个(以依赖于实现的方式)。这可能不是您想要的,因为计算机在自由选择时往往会做出错误的决定。

It's most certainly possible to have two methods with the same @Path annotation, e.g., if they're distinguished by other means (such as HTTP method or @Consumes annotation). The @Path on a class acts as a default/root for the @Paths on the class's methods. Moreover, it's not a problem at all if you've got one path that is “within” another; JAX-RS specifies that the most specific match possible is used. (I prefer to not do it that way, instead having the “outer” class return a reference to the “inner” class on a suitable partial match, so that every path has a traceable route to responsibility that definitely leads to a single class. That requires a fairly different way of arranging the @Path annotations though.)

But if you've ended up with two methods that can serve the same incoming request, you've got a clash and the JAX-RS implementation will be free to pick which one to use (in an implementation-dependent manner). That's probably not what you want, as computers tend to make bad decisions when given a free choice.

偏爱自由 2024-11-09 00:44:28

您可以使用以下配置实现拥有 /service/library/mynewlibrary 的目标。

在现有的类中,您在类级别配置了 /service/library,因此您可以在要添加的新类中在类级别配置 /service,然后在方法级别配置 /library/mynewlibrary。

这样,两个班级就不会有相同的路径,并且您的目标也达到了。我尝试过这个并且有效。

You can achieve your goal to have /service/library/mynewlibrary using below configuration.

In your existing class you have /service/library configured at class level so you can configure /service at class level in new class you are adding and then at method level configure /library/mynewlibrary.

This way it will not have same path for both classes and your goal is also achieved. I tried this and it works.

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