Spring mvc Spring MVC的每个控制器的默认mime类型';内容协商
有没有办法为使用Spring内容协商功能的SpringMVC控制器配置默认mime类型,即 ControllerA-我希望默认mime类型是JSON,所以Spring mvc Spring MVC的每个控制器的默认mime类型';内容协商,spring-mvc,serialization,content-negotiation,Spring Mvc,Serialization,Content Negotiation,有没有办法为使用Spring内容协商功能的SpringMVC控制器配置默认mime类型,即 ControllerA-我希望默认mime类型是JSON,所以http://mycompany.com/myresourceA将返回JSON,如果需要XML,则必须添加扩展名http://mycompany.com/myresourceA.xml ControllerB-我希望默认的mimetype是XML,所以http://mycompany.com/myresourceB将返回XML,如果我想要JSO
http://mycompany.com/myresourceA
将返回JSON,如果需要XML,则必须添加扩展名http://mycompany.com/myresourceA.xml
ControllerB-我希望默认的mimetype是XML,所以http://mycompany.com/myresourceB
将返回XML,如果我想要JSON,我必须添加扩展名http://mycompany.com/myresourceB.json
在我的contentNegotiationManagerBean
中,我将默认mime类型设置为XML,但这是一个全局配置
<property name="defaultContentType" value="application/xml" />
无法为整个控制器设置mime类型。您可以使用ResponseEntity作为操作方法的返回类型为操作设置它,然后为该操作设置响应类型 请参阅有关文档的更多信息: 使用ResposneEntity响应JSON的示例:
@RequestMapping(method=RequestMethod.GET, value="/fooBar")
public ResponseEntity<String> fooBar2() {
String json = "jsonResponse";
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity<String>(json, responseHeaders, HttpStatus.CREATED);
}
@RequestMapping(method=RequestMethod.GET,value=“/fooBar”)
公共响应食品2(){
字符串json=“jsonResponse”;
HttpHeaders responseHeaders=新的HttpHeaders();
setContentType(MediaType.APPLICATION_JSON);
返回新的ResponseEntity(json,responseHeaders,HttpStatus.CREATED);
}