31

If a request is sent to my API without an Accept header, I want to make JSON the default format. I have two methods in my controller, one for XML and one for 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.  
 }

When I send a request without an Accept header the getXmlData method is called, which is not what I want. Is there a way to tell Spring MVC to call the getJsonData method if no Accept header has been provided?

EDIT:

There is a defaultContentType field in the ContentNegotiationManagerFactoryBean that does the trick.

1
  • 3
    If you have found a solution using ContentNegotiationManagerFactoryBean add it as a solution. Commented Aug 12, 2013 at 16:25

2 Answers 2

39

From the Spring documentation, you can do this with Java config like this:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.defaultContentType(MediaType.APPLICATION_JSON);
  }
}

If you are using Spring 5.0 or later, implement WebMvcConfigurer instead of extending WebMvcConfigurerAdapter. WebMvcConfigurerAdapter has been deprecated since WebMvcConfigurer has default methods (made possible by Java 8) and can be implemented directly without the need for an adapter.

3
  • 2
    Just a detail: in a Spring boot application, your @Configuration class should not contain the @EnableWebMvc annotation (source). It may prevent other things from working, such as the springfox-swagger-ui html page. Commented May 30, 2018 at 17:37
  • 4
    WebMvcConigurerAdapter is deprecated now, just have to change extends WebMvcConfigurerAdapter to implements WebMvcConfigurer Commented Nov 30, 2018 at 15:40
  • Downvoted for misuse of @EnableWebMvc. Spring Boot shouldn't mix with it. Commented Jul 25, 2020 at 5:49
13

If you use spring 3.2.x, just add this to spring-mvc.xml

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false"/>
    <property name="mediaTypes">
        <value>
            json=application/json
            xml=application/xml
        </value>
    </property>
    <property name="defaultContentType" value="application/json"/>
</bean>
4
  • I put this in my servlet-context.xml and it worked perfectly. Thanks @Larry Z.
    – WalkerDev
    Commented Aug 13, 2013 at 17:47
  • Does setting mediaTypes has any effect when favorPathExtension is set to false?
    – holmis83
    Commented Feb 24, 2014 at 11:59
  • 2
    mediaTypes is used when favorParameter is set to true. This will check for query params, so /blahblah/4?format=xml would resolve to application/xml. The default value for favorParameter is false, so in this example setting mediaTypes has no effect. Commented Oct 30, 2014 at 19:04
  • 2
    Hi, after including the above code i am getting json as default if no accept headers. but if i set as application/xml . still im getting the response as json instead of XML?what im missing ?
    – Sundar G
    Commented Nov 22, 2017 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.