In my Spring XML I have the following snippet:
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="useDefaultSuffixPattern" value="false"/>
</bean>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="objectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
From what I understand, this means that Spring should NOT register "abc.*" and "abc/" when I have a mapping for "abc".
In one of my controllers I have a method which writes an image to the response:
@RequestMapping(value="{path}", method=RequestMethod.GET, produces=MediaType.IMAGE_PNG_VALUE)
@ResponseBody
public void getPath(
@PathVariable String path,
HttpServletResponse res) {
...
}
This works great when I request something like "abc", but when I request "abc.com" it throws a 406 error with the text:
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers."
When I request "abc.img", the "path" parameter only receives the text "abc"; Spring omits the extension.
It seems Spring is not correctly ignoring the suffix pattern. Why is this?
Edit: I translated the java config from Dirk's comment, and the following XML seems to fix this issue:
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
</bean>
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="objectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
I'm still not sure why the original code I had was not working, but this has resolved my issue
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…