title: POST JSON Object via RestTemplate in Spring Boot
body: <p>I am trying to POST a JSON object to an API endpoint which accepts the data in below format</p>
  
  <pre><code>{
      "names": [
          "name1",
          "name2",
          "name3"
      ]
  }
  </code></pre>
  
  <p>And my post method is as below</p>
  
  <pre><code>public String post(List&lt;String&gt; names) {
  
      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.APPLICATION_JSON);
  
      JSONObject jsonObject = new JSONObject();
      jsonObject .put("names", names);
  
      HttpEntity&lt;JSONObject&gt; entity = new HttpEntity&lt;&gt;(jsonObject , headers);
  
      return restTemplate.postForObject(Constants.URL, entity, String.class);
  }
  </code></pre>
  
  <p>When I call the post method I get this error</p>
  
  <pre><code>org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request
  </code></pre>
  
  <p>I printed out the <code>jsonObject</code> and tried to post it via Postman, it worked.</p>
  
  <p>What is the point that I am missing here?</p>
  
  <p>Any help would be appreciated.</p>