Jax-RS 中可选的 @PathParam

发布于 2024-10-26 09:25:22 字数 320 浏览 6 评论 0原文

我有一项服务,路径的最后部分是可选的,用户可以输入 /mypath//mypath/param1/

我尝试使用正则表达式来过滤路径的最后部分:

@Path("/mypath{param1: (/param1)?}")

我正在使用 RestEasy 作为我的 JAX-RS提供程序和代码在 Tomcat 中按预期工作,但是当我在 JBoss 中部署它时,如果我不提交可选部分,我会收到 405 返回代码。

我在这里做错了什么或者不可能以可移植的方式实现这一点?

I have a service where the last part of the path is optional, the user can both enter /mypath/ and /mypath/param1/.

I tried to use a regular expression to filter the last part of the path:

@Path("/mypath{param1: (/param1)?}")

I'm using RestEasy as my JAX-RS provider and the code works as expected in Tomcat but when I deploy it in JBoss I get a 405 return code when I do not submit the optional part.

Am I doing something wrong here or it's not possible to accomplish this in a portable way?

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

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

发布评论

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

评论(4

入怼 2024-11-02 09:25:22

问题是冒号之前缺少空格:

@Path("/mypath{param1: (/param1)?}")

应该是:

@Path("/mypath{param1 : (/param1)?}")

显然这是一个错误,因为 规范 使冒号周围的空格可选。我还发现我不是第一个 被这个虫子咬了

The problem was the lack of whitespace before the colon:

@Path("/mypath{param1: (/param1)?}")

should be:

@Path("/mypath{param1 : (/param1)?}")

Apparently it's a bug, because the specification makes the whitespace around the colon optional. I also found that I'm not the first bitten by this bug.

大海や 2024-11-02 09:25:22

就我而言,我必须使用其他表达式:

@Path('/mypath/{param1 : (\\w+)?}')

否则,您必须清理参数。

In my case I had to use this other expression:

@Path('/mypath/{param1 : (\\w+)?}')

Otherwise you have to clean the parameter.

一身仙ぐ女味 2024-11-02 09:25:22

验证是否已使用 /mypath 定义了接受不同方法的路径,这可能是您收到 405(不允许方法)的原因。另外,当您有可选参数时,我想最好让它们成为查询参数。

Verify whether there is a path already defined with /mypath that accepts a different method, this could be the reason why you are getting 405 (Method not allowed) back. Also when you have optional parameters I guess it is better to make them query parameters.

箜明 2024-11-02 09:25:22

使用 Dropwizard 1.0 和 Java8,您可以使用Optional,

@Path("/news")
getLastNews(@QueryParam("topicId") String topicId, @QueryParam("limit") Optional<Integer> limit) 

它将同时响应

/news?topicId=123213?limit=200

/news?topicId=123213

With Dropwizard 1.0 and Java8 you can use Optional

@Path("/news")
getLastNews(@QueryParam("topicId") String topicId, @QueryParam("limit") Optional<Integer> limit) 

It will give a response to both

/news?topicId=123213?limit=200

and

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