Spring Web Controller 在扩展名下返回 XML.html



我正在编写一个Controller来替换提供 xml 文档的遗留系统 - 但 url 具有.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 配置发现了一个类似的问题/答案。对于基于注释的配置,您只需要添加此配置类

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);
}
}

最新更新