Trying to create REST-ful URLs with multiple dots

2019-01-11 04:13发布

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

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.

I've unsuccessfully looked for a way of overriding the default file extension detection in Spring.

Is it possible to customize or disable file extension detection for a given controller method or URL pattern, etc?

7条回答
时光不老,我们不散
2楼-- · 2019-01-11 04:57

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.

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.

查看更多
登录 后发表回答