在 Jersey 中对不匹配的 REST 方法使用默认方法

发布于 2024-09-30 22:22:07 字数 316 浏览 1 评论 0原文

我知道这不太“安静”,但是我想了解是否以及如何处理与 REST 资源中的任何方法不匹配的所有请求(我想将这些请求代理到另一台服务器)。例如,它可以是这样的方法:

@GET 
@Path("*") 
public Response defaultMethod(@Context HttpServletRequest request, @Context HttpServletResponse response) 
{ 
    // do proxying here 
} 

我怎样才能实现这个目标?

BR,

塞尔坎C

I know this is not quite "restful" but, I want to learn if and how I can handle all requests which do not match any of the methods in my REST resource (I want to proxy these requests to another server). For example, it could be a method like:

@GET 
@Path("*") 
public Response defaultMethod(@Context HttpServletRequest request, @Context HttpServletResponse response) 
{ 
    // do proxying here 
} 

how can I achieve this?

BR,

SerkanC

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

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

发布评论

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

评论(2

荒路情人 2024-10-07 22:22:07
@Path("/{default: .*}")

这适用于:

  • http://example.com/someUnexpectedPath
  • http://example.com/someUnexpectedPath/withAdditionalSubPaths
  • http://example.com/someUnexpectedPath /withAdditionalSubPaths?andParams=whatever
@Path("/{default: .*}")

This works for:

  • http://example.com/someUnexpectedPath
  • http://example.com/someUnexpectedPath/withAdditionalSubPaths
  • http://example.com/someUnexpectedPath/withAdditionalSubPaths?andParams=whatever
腹黑女流氓 2024-10-07 22:22:07

一种可能的选择是定义自定义 404 映射。由于 404s 处理所有不匹配的请求,因此它应该捕获所有未定义的 url。

我将以下内容添加到我的 web.xml 文件中

 <error-page>
    <error-code>404</error-code>
    <location>/error/404</location>
  </error-page>

,其中 location 是您要将请求定向到的路径。然后只需将调用定义为正常即可。

One possible option would be to define a custom 404 mapping. Since 404s handles all unmatched requests it should catch all undefined urls.

I added the following this to my web.xml file

 <error-page>
    <error-code>404</error-code>
    <location>/error/404</location>
  </error-page>

Where location is the path you want to direct the request to. Then just define that the call as normal.

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