2013-06-26 74 views
2

是否有配置为使用Spring的内容协商功能,一个Spring MVC控制器默认的MIME类型的方式,即默认的MIME类型

ControllerA - 我想要的默认的MIME类型为JSON,所以http://mycompany.com/myresourceA将返回JSON,如果我想XML我要补充扩展http://mycompany.com/myresourceA.xml

ControllerB - 我想默认的MIME类型是XML,所以http://mycompany.com/myresourceB将返回XML,如果我想JSON我要添加扩展http://mycompany.com/myresourceB.json

I ñ我contentNegotiationManagerBean我有默认的MIME类型设置为XML,但是这是一个全局配置

<property name="defaultContentType" value="application/xml" /> 
+0

你解决这个问题? –

回答

0

有没有办法来设置整个控制器的MIME类型。您可以使用ResponseEntity将其设置为您的操作,作为您的操作方法的返回类型,然后设置该操作的响应类型。

查看更多的文档:http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-httpentity

样品为responsing与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); 
    }