title: Spring HATEOAS and mockMVC, ignores consumes = MediaType.APPLICATION_JSON_UTF8_VALUE
body: <p>After creating a service using Spring HATEOAS and testing it with mockmvc (also to generate documentation using Spring restdocs) we found the following.</p>
  
  <p>Our RestController looks something like:</p>
  
  <pre><code>@RestController
  @RequestMapping("/v1/my")
  @ExposesResourceFor(My.class)
  @EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
  public class MyController {
  
     @Autowired
     private MyRepository myRepository;
  
     @Autowired
     private MyResourceAssembler myResourceAssembler;
  
     @RequestMapping(path = "", method = POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
     public HttpEntity&lt;Resource&lt;MyResource&gt;&gt; addMy(
          @RequestBody MyResource newMyResource) {
  
      if (myRepository.existsByMyId(newMyResource.getMyId())) {
          return new ResponseEntity&lt;&gt;(HttpStatus.CONFLICT);
      }
  
      val newMy = new My(
              null, // generates id
              newMy.getMyId()
      );
  
      val myResource = myResourceAssembler.toResource(myRepository.save(newMy));
      val resource = new Resource&lt;&gt;(myResource);
  
      return new ResponseEntity&lt;&gt;(resource, HttpStatus.CREATED);
     }
  }
  </code></pre>
  
  <p>To test this we have created the following test:</p>
  
  <pre><code>@Test
  public void addMy() throws Exception {
      this.mockMvc.perform(post("/v1/my")
              .content("{ \"myId\": 9911 }")
              .contentType(MediaType.APPLICATION_JSON_UTF8)
              .accept(MediaTypes.HAL_JSON))
              .andExpect(status().isCreated())
              .andExpect(MockMvcResultMatchers.jsonPath("$.myId").value(9911))   
              .andDo(this.document.document(selfLinkSnippet, responseFieldsSnippet));
  }
  </code></pre>
  
  <p>The unit test result we get is:</p>
  
  <pre><code>java.lang.AssertionError: Status 
  Expected :201
  Actual   :415
  </code></pre>
  
  <p>Status code 415 is Media Type unsupported.</p>
  
  <p>If we modify the unit test to say:</p>
  
  <pre><code>.contentType(MediaTypes.HAL_JSON)
  </code></pre>
  
  <p>The unit test returns success. Which is strange since we indicated to consume only application/json. 
  The problem now is that the documentation that is generated, incorrectly states that the POST request should use Content-type application/hal+json:</p>
  
  <pre><code>curl 'http://my-service/v1/my' -i -X POST -H 'Content-Type: application/hal+json' -H 'Accept: application/hal+json' -d '{ "myId": 9911 }'
  </code></pre>
  
  <p>And if you try that, you will get another 415.
  If we change the Content-Type: to application/json, then it does work.</p>
  
  <p>If we tell the method to consume both HAL+JSON and JSON:</p>
  
  <pre><code>@RequestMapping(path = "", method = POST, consumes = { MediaTypes.HAL_JSON_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE})
  </code></pre>
  
  <p>Then the unit test succeeds when contentType(MediaTypes.HAL_JSON) is set, but fails when contentType(MediaType.APPLICATION_JSON_UTF8) is set.</p>
  
  <p>However, now the service both accepts application/json AND application/hal+json, such that the documentation is at least working.</p>
  
  <p>Does anyone know why this is?</p>