title: Correct JSON REST Controller by Spring
body: <p>I am new in writing spring rest controller. I want to create a simple sending in JSON format, but I can't deal with it, is my code a correct code for it?</p>
  
  <p>In my code, there is a RestTemplate, which sends a simple POJO to the REST url, and the REST Controller sends back another POJO.</p>
  
  <p>I found a lot of examples sending and receiving objects as JSON, but some of them are a few years old. The most I found is where they configure the dispatcher bean XML by adding <code>MappingJackson2HttpMessageConverter</code> for <em>Configure bean to convert JSON to POJO and vice versa</em>:</p>
  
  <pre><code>...
  &lt;beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"&gt;
      &lt;beans:property name="messageConverters"&gt;
          &lt;beans:list&gt;
              &lt;beans:ref bean="jsonMessageConverter"/&gt;
          &lt;/beans:list&gt;
      &lt;/beans:property&gt;
  &lt;/beans:bean&gt;
  &lt;beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"&gt;
  &lt;/beans:bean&gt; 
  ...
  </code></pre>
  
  <p>And also they set it to the RestTemplate java code:</p>
  
  <pre><code>List&lt;HttpMessageConverter&lt;?&gt;&gt; messageConverters = new ArrayList&lt;HttpMessageConverter&lt;?&gt;&gt;();
  messageConverters.add(new MappingJackson2HttpMessageConverter());
  messageConverters.add(new StringHttpMessageConverter());
  restTemplate.setMessageConverters(messageConverters);
  </code></pre>
  
  <p>Sometimes they set a header too:</p>
  
  <pre><code>HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.APPLICATION_JSON);
  headers.add("Accept", "application/json");
  HttpEntity&lt;MyObject&gt; entity = new HttpEntity&lt;MyObject&gt;(inputRequest, headers);
  </code></pre>
  
  <p>Sometimes they convert their object to JSON format, and send it as a text, not the instance.</p>
  
  <p>And also I can found 2 ways for sending a POST message:</p>
  
  <pre><code>ResponseEntity&lt;MyObject&gt; responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, MyObject.class);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>MyObject response = restTemplate.postForObject(url, inputRequest, MyObject.class);
  </code></pre>
  
  <p>And this is the REST Controller:</p>
  
  <pre><code>@RequestMapping(value = "/send", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
  @ResponseBody
  public MyObject send(@RequestBody MyObject requestModel) {
      return //whatever;
  }
  </code></pre>
  
  <p>BUT: if I don't set anything into the XML, and don't add any message converter and header to the RestTemplate, it looks like it working fine. I test it by PostMan, and if I add a JSON format of my MyClass example, I receive JSON.</p>
  
  <p><strong><em>So my question is: Is my code correct this way for JSON sending really?:</em></strong></p>
  
  <p><strong>mvc-dispatcher.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:beans="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
      &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
      &lt;annotation-driven /&gt;
      &lt;!-- to reach resources like css and images --&gt;
      &lt;default-servlet-handler/&gt;
      &lt;!-- the REST controllers are here --&gt;
      &lt;context:component-scan base-package="hu.viktor" /&gt;
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p><strong>RestTemplate java:</strong></p>
  
  <pre><code>@Controller
  public class RequestSender {
      public MyObject send(MyObject inputRequest) {
          RestTemplate restTemplate = new RestTemplate();
          String url = "http://localhost:8080/rest/send";
          MyObject response = restTemplate.postForObject(url, inputRequest, MyObject.class);
          return response;
      }
  }
  </code></pre>
  
  <p><strong>And the REST Controller:</strong></p>
  
  <pre><code>@Controller
  @RequestMapping("/rest")
  public class CalculatorRestController {
      @RequestMapping(value = "/send", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
      @ResponseBody
      public MyObject send(@RequestBody MyObject requestModel) {
          return //whatever;
      }   
  }
  </code></pre>