Spring MVC - Setting Default Content Type with ContentNegotiationConfigurer

[Last Updated: Jul 20, 2017]

In Spring MVC, if none of the enabled ContentNegotiationStrategies can determine the requested content type then 406 code (Not Acceptable) is returned. One way to avoid that; we can set a default content type to be used. In the following example, we are going to set json as default media type by using ContentNegotiationConfigurer#defaultContentType() method.


Setting default content type

public class MyWebConfig extends WebMvcConfigurerAdapter {
  public void configureContentNegotiation (ContentNegotiationConfigurer configurer) {

Writing Controller

public class UserController {
  public User getUserById (@RequestParam("id") long userId) {
      //creating dummy user
      User user = new User();
      return user;
public class User implements Serializable {
  private Long id;
  private String name;
  private String password;
  private String emailAddress;

Adding Jackson dependency

We also need to include Jackson dependency so that MappingJackson2HttpMessageConverter can be used to produce JSON response.



Writing JUnit tests

In this test, we are not going to use 'Accept' header, or path extension or request parameter ('format') so that our default JSON media type can be used to returned the response:

@ContextConfiguration(classes = MyWebConfig.class)
public class UserTests {
  private WebApplicationContext wac;
  private MockMvc mockMvc;
  public void setup () {
      DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.wac);
      this.mockMvc =;
  public void testUserRequest () throws Exception {
      MockHttpServletRequestBuilder builder =
                                      .param("id", "100");


mvn -q test -Dtest=UserTests#testUserRequest

HTTP Method = GET
Request URI = /user
Parameters = {id=[100]}
Headers = {}

Type = com.logicbig.example.UserController
Method = public com.logicbig.example.User com.logicbig.example.UserController.getUserById(long)

Async started = false
Async result = null

Resolved Exception:
Type = null

View name = null
View = null
Model = null

Attributes = null

Status = 200
Error message = null
Headers = {Content-Type=[application/json;charset=UTF-8]}
Content type = application/json;charset=UTF-8
Body = {"id":100,"name":"joe","password":null,"emailAddress":""}
Forwarded URL = null
Redirected URL = null
Cookies = []

As seen in the output, we got JSON output in the body.

Note that, the browser (like Chrome) will always have XML response, that's because the browser implicitly sends multiple 'Accept' headers in the request which includes XML media type as well. That means HeaderContentNegotiationStrategy will be utilized before our default content types will be used.

Example Project

Dependencies and Technologies Used:

  • spring-webmvc 4.3.10.RELEASE: Spring Web MVC.
  • spring-test 4.3.10.RELEASE: Spring TestContext Framework.
  • javax.servlet-api 3.1.0 Java Servlet API
  • jackson-databind 2.8.9: General data-binding functionality for Jackson: works on core streaming API.
  • junit 4.12: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.
  • JDK 1.8
  • Maven 3.3.9

Setting Default Content Type Example Select All Download
  • content-negotiation-default-content-type
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • test
          • java
            • com
              • logicbig
                • example

    See Also