尝试在“文件名”中创建具有多个点的REST-ful URL。 part - 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.
但是,我的某些网址要求名称中带有点的标识符。例如。像这样:
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?
推荐答案
对于URL中的点, @PathVariable
模式匹配有点抽搐(参见 SPR-5778 )。你可以通过设置 useDefaultSuffixPattern
property on 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。 part - Spring 3.0 MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!