title: Can not attach body to my POST request using Spring MockMvc
body: <p>I'm trying to test my rest controller. No issues with GETs, but when I try to test a POST method I'm unable to attach the body.</p>
  
  <pre><code>private static final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
                                                              MediaType.APPLICATION_JSON.getSubtype(),
                                                              Charset.forName("utf8"));
  private ObjectMapper jsonMapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
  
  @Test
  public void test1() throws Exception {
      //...Create DTO
      //...Create same pojo but as entity
  
      when(serviceMock.addEntity(e)).thenReturn(e);
  
      mvc.perform(post("/uri")
          .contentType(contentType)
          .content(jsonMapper.writeValueAsString(dto))
          )
          .andDo(print())
          .andExpect(status().isCreated())
          .andExpect(content().contentType(contentType)); //fails because there is no content returned
  }
  </code></pre>
  
  <p>This is the request output:</p>
  
  <pre><code>MockHttpServletRequest:
        HTTP Method = POST
        Request URI = /uri
         Parameters = {}
            Headers = {Content-Type=[application/json;charset=UTF-8]}
  </code></pre>
  
  <p>There is no body. Why? I have printed <code>jsonMapper.writeValueAsString(dto)</code> and is not null.</p>
  
  <p>edit:</p>
  
  <p>Adding controller code:</p>
  
  <pre><code>@RestController
  @RequestMapping("/companies")
  public class CompanyController {
  
      @Autowired
      private CompanyService service;
      @Autowired
      private CompanyMapper mapper;
  
  
  
      @RequestMapping(method=RequestMethod.GET)
      public List&lt;CompanyDTO&gt; getCompanies() {
          List&lt;Company&gt; result = service.getCompanies();
          return mapper.toDtoL(result);
      }   
  
      @RequestMapping(method=RequestMethod.POST)
      @ResponseStatus(HttpStatus.CREATED)
      public CompanyDTO createCompany(@RequestBody @Valid CompanyDTO input) {
          Company inputE = mapper.toEntity(input);
          Company result = service.addCompany(inputE);
          return mapper.toDto(result);
      }
  </code></pre>