title: Send a String to a Spring(Jackson) REST controller in a POST method
body: <p>I am attempting to send a <code>String</code> object to a rest service using HTTP Post method. and the String should be sent in the request body.</p>
  
  <p>The controller</p>
  
  <pre><code>@RestController
  @RequestMapping(value = "post", method = RequestMethod.POST)
  public class HttpMethodPostController {
  
      /*HttpClientErrorException: 415 null*/
      @RequestMapping(value = "/string_as_text", consumes = MediaType.TEXT_PLAIN_VALUE)
      public ResponseEntity&lt;Void&gt; getStringAsText(@RequestBody String text) {
          System.out.println("[Server] text = " + text);
          return new ResponseEntity&lt;Void&gt;(HttpStatus.OK);
      }
  
      /*HttpClientErrorException: 400 null*/
      @RequestMapping(value = "/string_as_json", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
      public ResponseEntity&lt;Void&gt; getStringAsJSON(@RequestBody String text) {
          System.out.println("[Server] text = " + text);
          return new ResponseEntity&lt;Void&gt;(HttpStatus.OK);
      }
  
      @RequestMapping(value = "/type1_", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
      public ResponseEntity&lt;Void&gt; postType1_(@RequestBody Type1_ type1_) {
          System.out.println("[Server] type1_ = " + type1_);
          return new ResponseEntity&lt;Void&gt;(HttpStatus.OK);
      }
  
  }
  </code></pre>
  
  <p>The consumer</p>
  
  <pre><code>public class HttpMethodPostConsumer {
      public static final String POST_ADDRESS = "http://localhost:55055/post";
  
      /*HttpClientErrorException: 415 null*/
      public static void postStringAsText(String text) {
          final RestTemplate restTemplate = new RestTemplate();
          final HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.TEXT_PLAIN);
          final HttpEntity&lt;String&gt; entity = new HttpEntity&lt;&gt;(text, headers);
          final URI uri = restTemplate.postForLocation(POST_ADDRESS + "/string_as_text", entity, String.class);
          System.out.println("uri = " + uri);
      }
  
      /*HttpClientErrorException: 400 null*/
      public static void postStringAsJSON(String text) {
          final RestTemplate restTemplate = new RestTemplate();
          final HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
          final HttpEntity&lt;String&gt; entity = new HttpEntity&lt;&gt;(text, headers);
          final URI uri = restTemplate.postForLocation(POST_ADDRESS + "/string_as_json", entity, String.class);
          System.out.println("uri = " + uri);
      }
  
      public static void postType1_(Type1_ type1_) {
          final RestTemplate restTemplate = new RestTemplate();
          final HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
          final HttpEntity&lt;Type1_&gt; httpEntity = new HttpEntity&lt;Type1_&gt;(type1_, headers);
          final URI result_URI = restTemplate.postForLocation(POST_ADDRESS + "/type1_", httpEntity, Type1_.class);
          System.out.println("result_URI = " + result_URI);
      }
  }
  </code></pre>
  
  <p>The consumer test runner</p>
  
  <pre><code>public class HttpMethodPostConsumerTest {
  
      /*HttpClientErrorException: 415 null*/
      @Test
      public void test_postStringAsText() {
          final String text = Randomizers.getString();
          System.out.println("text = " + text);
          postStringAsText(text);
      }
  
      /*HttpClientErrorException: 400 null*/
      @Test
      public void test_postStringAsJSON() {
          final String text = Randomizers.getString();
          System.out.println("text = " + text);
          postStringAsJSON(text);
      }
  
      @Test
      public void test_postType1_() {
          final Type1_ type1_ = ModelFactory.getType1_();
          System.out.println("[Consumer] type1_ = " + type1_);
          postType1_(type1_);
      }
  
  }
  </code></pre>
  
  <p><strong>The Testing Procedure</strong></p>
  
  <ol>
  <li>The server is run first (<a href="http://localhost:55055/" rel="nofollow noreferrer">http://localhost:55055/</a>)</li>
  <li>Wait until the server is up and running.</li>
  <li>Run <code>test_postType1_()</code> will work as expected.</li>
  <li>Run <code>test_postStringAsText()</code> Will result in client/consumer side error</li>
  </ol>
  
  <blockquote>
    <p>org.springframework.web.client.HttpClientErrorException: 415 null</p>
    
    <p>at
    org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
      at
    org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79)
      at
    org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
      at
    org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:775)
      at
    org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:728)
      at
    org.springframework.web.client.RestTemplate.execute(RestTemplate.java:684)
      at
    org.springframework.web.client.RestTemplate.postForLocation(RestTemplate.java:405)
      at
    personal.learn.java.spring_rest.rest_general.non_automated.consumers.HttpMethodPostConsumer.postStringAsText(HttpMethodPostConsumer.java:21)
      at
    personal.learn.java.spring_rest.rest_general.non_automated.consumers.HttpMethodPostConsumerTest.test_postStringAsText(HttpMethodPostConsumerTest.java:17)</p>
  </blockquote>
  
  <ol>
  <li>Run <code>test_postStringAsJSON()</code> will result in errors
  
  <ul>
  <li>Client/consumer side error</li>
  </ul></li>
  </ol>
  
  <blockquote>
    <p>org.springframework.web.client.HttpClientErrorException: 400 null</p>
    
    <p>at
    org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
      at
    org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79)
      at
    org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
      at
    org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:775)
      at
    org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:728)
      at
    org.springframework.web.client.RestTemplate.execute(RestTemplate.java:684)
      at
    org.springframework.web.client.RestTemplate.postForLocation(RestTemplate.java:405)
      at
    personal.learn.java.spring_rest.rest_general.non_automated.consumers.HttpMethodPostConsumer.postStringAsJSON(HttpMethodPostConsumer.java:31)
      at
    personal.learn.java.spring_rest.rest_general.non_automated.consumers.HttpMethodPostConsumerTest.test_postStringAsJSON(HttpMethodPostConsumerTest.java:25)</p>
  </blockquote>
  
  <p>Server side error (In console)</p>
  
  <blockquote>
    <p>02-May-2018 17:25:45.469 WARNING [http-nio-55055-exec-2]
    org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotReadable
    Failed to read HTTP message:
    org.springframework.http.converter.HttpMessageNotReadableException:
    JSON parse error: Unrecognized token 'IAtQDIxTCh': was expecting
    'null', 'true', 'false' or NaN; nested exception is
    com.fasterxml.jackson.core.JsonParseException: Unrecognized token
    'IAtQDIxTCh': was expecting 'null', 'true', 'false' or NaN  at
    [Source: (PushbackInputStream); line: 1, column: 21]</p>
  </blockquote>
  
  <p>Server side error (In Tomcat Catalina Log)</p>
  
  <blockquote>
    <p>02-May-2018 17:25:45.469 WARNING [http-nio-55055-exec-2]
    org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotReadable
    Failed to read HTTP message:
    org.springframework.http.converter.HttpMessageNotReadableException:
    JSON parse error: Unrecognized token 'IAtQDIxTCh': was expecting
    'null', 'true', 'false' or NaN; nested exception is
    com.fasterxml.jackson.core.JsonParseException: Unrecognized token
    'IAtQDIxTCh': was expecting 'null', 'true', 'false' or NaN  at
    [Source: (PushbackInputStream); line: 1, column: 21]</p>
  </blockquote>