Spring does not ignore file extension

拟墨画扇 提交于 2019-11-29 08:01:32
Dirk Lachowski

When a request comes in to the spring dispatcher, part of the controller mapping/matching is matching accepted media types on the client side to producible media types on the controller side (so you can have controllers that are only distinguishable by their produced media types).

The bad news is that springmvc in its default configuration favors the extension of the requested url over any accept header in the request.

In your example, when you are requesting abc there is no match on the extension so other contentent negotiation strategies are kicking in eventually resolving to the right type (via accept header). But if you request abc.com spring will derive a mime type of application/octet-stream that doesn't match the produces of your controller and generating a 406 (because there is no matching controller).

You can find the default mime matching for path extensions in the spring-context-support.jar in org/springframework/mail/javamail/mime.types (see https://github.com/spring-projects/spring-framework/blob/master/spring-context-support/src/main/resources/org/springframework/mail/javamail/mime.types#L278 ).

You can disable this 'feature' in your dispatcher config so spring will not use the path extension to resolve the mime-type:

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
</bean>

If you are using java config have a look at my related question/answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!