如果在没有Accept标头的情况下将请求发送到我的API,我想将JSON设置为默认格式.我的控制器中有两个方法,一个用于XML,另一个用于JSON:
@RequestMapping(method = RequestMethod.GET,produces=MediaType.APPLICATION_ATOM_XML_VALUE)
@ResponseBody
public ResponseEntity<SearchResultResource> getXmlData(final HttpServletRequest request) {
//get data, set XML content type in header.
}
@RequestMapping(method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<Feed> getJsonData(final HttpServletRequest request){
//get data, set JSON content type in header.
}
Run Code Online (Sandbox Code Playgroud)
当我发送没有Accept标头的请求时,getXmlData会调用该方法,这不是我想要的.getJsonData如果没有提供Accept标头,有没有办法告诉Spring MVC调用该方法?
编辑:
有一个defaultContentType领域ContentNegotiationManagerFactoryBean可以解决问题.