Spring MVC:RESTful URI 的控制器方法的正确注释,包括 ';'
设计我的 RESTful API,我想使用以下 URI,
http://[HOST]/[PLANET]/[LAT];[LONG]
例如
http://myserver/earth/50.2;29.1
Spring MVC 中此类方法的适当注释是什么?下面这个可以吗?
@RequestMapping(value = "/{planet}/{lat};{long}", method = RequestMethod.GET)
public String showInfoAboutCoords(
@PathVariable final String planet,
@PathVariable final String lat,
@PathVariable final String long,
final HttpServletResponse response) {
// Implementation
}
如果这个没问题 - @MaskFormat("###-##-####")
有什么用?
Designing my RESTful API, I would like use following URI
http://[HOST]/[PLANET]/[LAT];[LONG]
e.g.
http://myserver/earth/50.2;29.1
What is the appropiate annotation of a such a method in Spring MVC? Is this the following one ok?
@RequestMapping(value = "/{planet}/{lat};{long}", method = RequestMethod.GET)
public String showInfoAboutCoords(
@PathVariable final String planet,
@PathVariable final String lat,
@PathVariable final String long,
final HttpServletResponse response) {
// Implementation
}
If this one is ok - what is @MaskFormat("###-##-####")
good for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 URI 模式有两个问题:
;
视为分隔符并修剪 URI(例如 Tomcat 的 错误 30535)。因此,作为解决方法,您可以使用一些不同的字符,例如,
。因此,您将看到类似的内容
,请注意,由于您禁用了 Spring 的扩展处理,因此如果需要,您必须手动启用它(这还需要更严格的正则表达式,以避免将小数点与扩展分隔符混淆)
: 您可能指的是来自
mvc-showcase
的注释(请注意,它是内置注释)。它与 MaskFormatAnnotationFormatterFactory 一起演示了将路径变量(即字符串)转换为方法参数的新格式化工具。实际上,它将String
转换为String
,因此它仅用于验证。Your URI pattern has two problems:
;
as a delimiter and trim the URI (for example Tomcat's bug 30535). So, as a workaround you may use some different character, like,
.So, you'll have something like
Note that since you disabled Spring's extension handling, you have to enable it manually if you need it (this also requires more restrictive regexp to avoid confusing decimal point with extension separator):
By
@MaskFormat
you probably mean an annotation frommvc-showcase
(note that it's note a built-in annotation). Along withMaskFormatAnnotationFormatterFactory
it demonstrates the new formatting faclilities to convert path variables (i.e. strings) to method arguments. Actually it convertsString
s toString
s, so it's used only for validation.