Jax-RS 中可选的 @PathParam
我有一项服务,路径的最后部分是可选的,用户可以输入 /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是冒号之前缺少空格:
应该是:
显然这是一个错误,因为 规范 使冒号周围的空格可选。我还发现我不是第一个 被这个虫子咬了。
The problem was the lack of whitespace before the colon:
should be:
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.
就我而言,我必须使用其他表达式:
@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.
验证是否已使用 /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.
使用 Dropwizard 1.0 和 Java8,您可以使用Optional,
它将同时响应
和
With Dropwizard 1.0 and Java8 you can use Optional
It will give a response to both
and