尝试在“文件名"中创建具有多个点的 REST-ful URL部分 - Spring 3.0 MVC [英] Trying to create REST-ful URLs with multiple dots in the "filename" part - Spring 3.0 MVC
问题描述
我正在使用带有注释驱动控制器的 Spring MVC (3.0).我想为资源创建 REST-ful URL,并且能够不要求(但仍然可以选择允许)在 URL 末尾的文件扩展名(但如果没有扩展名,则假定为 HTML 内容类型).只要文件名部分中没有点(句点/句号),这对 Spring MVC 来说是开箱即用的.
I'm using Spring MVC (3.0) with annotation-driven controllers. I would like to create REST-ful URLs for resources and be able to not require (but still optionally allow) file extension on the end of the URL (but assume HTML content type if no extension). This works out-of-the-box with Spring MVC as long as there are no dots (period/full-stop) in the filename part.
但是,我的某些 URL 需要名称中带有点的标识符.例如.像这样:
However some of my URLs require an identifier with dots in the name. E.g. like this:
http://company.com/widgets/123.456.789.500
在这种情况下,Spring 会查找扩展名 .500
的内容类型,但没有发现任何错误.我可以使用变通方法,例如在末尾添加 .html
、对标识符进行编码或添加尾部斜杠.我对这些都不满意,但可能可以忍受添加 .html
.
In this case Spring looks for a content type for the extension .500
and finds none so errors. I can use work-arounds like adding .html
to the end, encoding the identifier or adding a trailing slash. I'm not happy with any if these but could probably live with adding .html
.
我没有成功寻找一种覆盖 Spring 中默认文件扩展名检测的方法.
I've unsuccessfully looked for a way of overriding the default file extension detection in Spring.
是否可以针对给定的控制器方法或 URL 模式等自定义或禁用文件扩展名检测?
Is it possible to customize or disable file extension detection for a given controller method or URL pattern, etc?
推荐答案
@PathVariable
模式匹配在涉及到 URL 中的点时有点棘手(请参阅 SPR-5778).您可以通过设置 useDefaultSuffixPattern
DefaultAnnotationHandlerMapping
到 false
.
The @PathVariable
pattern matching is a bit twitchy when it comes to dots in the URL (see SPR-5778). You can make it less twitchy (but more picky), and get better control over dot-heavy URLs, by setting the useDefaultSuffixPattern
property on DefaultAnnotationHandlerMapping
to false
.
如果您尚未在上下文中显式声明 DefaultAnnotationHandlerMapping
(并且大多数人没有,因为它是为您隐式声明的),那么您可以显式添加它,并设置该属性.
If you haven't already explicitly declared a DefaultAnnotationHandlerMapping
in your context (and most people don't since it's declared implicitly for you), then you can add it explicitly, and set that property.
这篇关于尝试在“文件名"中创建具有多个点的 REST-ful URL部分 - Spring 3.0 MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!