Spring MVC:RESTful URI 的控制器方法的正确注释,包括 ';'

发布于 2024-09-25 03:31:25 字数 587 浏览 0 评论 0原文

设计我的 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 技术交流群。

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

发布评论

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

评论(1

戒ㄋ 2024-10-02 03:31:25

您的 URI 模式有两个问题:

  • 某些 servlet 容器可能会将 ; 视为分隔符并修剪 URI(例如 Tomcat 的 错误 30535)。因此,作为解决方法,您可以使用一些不同的字符,例如 ,
  • 默认情况下,Spring MVC 将 URI 中的点视为扩展分隔符并对其进行修剪。您可以通过为路径变量指定正则表达式模式来覆盖它。

因此,您将看到类似的内容

@RequestMapping(value = "/{planet}/{lat:.*},{long:.*}", method = RequestMethod.GET) 

,请注意,由于您禁用了 Spring 的扩展处理,因此如果需要,您必须手动启用它(这还需要更严格的正则表达式,以避免将小数点与扩展分隔符混淆)

@RequestMapping(value = 
    {"/{planet}/{lat:.*},{long:\\d+\\.\\d+}", 
         "/{planet}/{lat:.*},{long:\\d+\\.\\d+}.*"}, 
    method = RequestMethod.GET)

: 您可能指的是来自 mvc-showcase 的注释(请注意,它是内置注释)。它与 MaskFormatAnnotationFormatterFactory 一起演示了将路径变量(即字符串)转换为方法参数的新格式化工具。实际上,它将 String 转换为 String,因此它仅用于验证。

Your URI pattern has two problems:

  • Some servlet containers may treat ; as a delimiter and trim the URI (for example Tomcat's bug 30535). So, as a workaround you may use some different character, like ,.
  • By default Spring MVC treats point in URI as an extension separator and trims it too. You can override it by specifying regexp pattern for path variable.

So, you'll have something like

@RequestMapping(value = "/{planet}/{lat:.*},{long:.*}", method = RequestMethod.GET) 

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):

@RequestMapping(value = 
    {"/{planet}/{lat:.*},{long:\\d+\\.\\d+}", 
         "/{planet}/{lat:.*},{long:\\d+\\.\\d+}.*"}, 
    method = RequestMethod.GET)

By @MaskFormat you probably mean an annotation from mvc-showcase (note that it's note a built-in annotation). Along with MaskFormatAnnotationFormatterFactory it demonstrates the new formatting faclilities to convert path variables (i.e. strings) to method arguments. Actually it converts Strings to Strings, so it's used only for validation.

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