.html扩展
问题描述:
我正在写一个Controller
,以取代旧系统供应的XML文档下的Spring Web控制器返回XML - 但是URL有.html
扩展.html扩展
我的控制器看起来像这样
@RequestMapping(value="/{name}.html",
method = RequestMethod.GET,
consumes = MediaType.ALL_VALUE,
produces = MediaType.APPLICATION_XML_VALUE)
public @ResponseBody XmlContent getContent(@PathVariable(value = "name") String name) {
return service.getXmlContent();
}
当我试着打了URL,我得到一个406错误:
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
如果我改变了请求映射为.xml
这一切工作正常。是否有我需要禁用的组件 - 拦截请求的.html
部分并拒绝将其映射到xml的东西?
答
我使用spring xml配置发现了一个类似的类似question/answer。对于基于注释的配置,您只需添加此配置类
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
}