2
votes

Is there a way to configure a default mime type for a Spring MVC controller that uses Spring's content negotiation feature, i.e.

ControllerA - I want the default mime type to be JSON, so http://mycompany.com/myresourceA will return JSON, if I want XML I have to add the extension http://mycompany.com/myresourceA.xml

ControllerB - I want the default mimetype to be XML, so http://mycompany.com/myresourceB will return XML, if I want JSON I have to add the extension http://mycompany.com/myresourceB.json

In my contentNegotiationManagerBean I have the default mime type set to XML but that is a global config

<property name="defaultContentType" value="application/xml" />
1

1 Answers

0
votes

There is no way to set the mime type for a whole controller. You can set it for your Actions using ResponseEntity as the return type for your action method, and then setting the response type for that action.

See more on documentation: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-httpentity

Sample for responsing a JSON with ResposneEntity:

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