2017-08-25 53 views
0

我正在寫一個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的東西?

回答

0

我使用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); 
    } 
}