title: Best practice to deserialize a JSON using Spring MVC
body: <p>I'm developing a web application using Spring MVC; it works as follows:</p>
  
  <ol>
  <li>a JSON request is created from a model object (a POJO)</li>
  <li>it's sent to an external server that exposes some APIs</li>
  <li>a JSON response is received by the controller</li>
  </ol>
  
  <p>I need to figure out which is the best way to serialize and deserialize JSON in a Java web application. At first I was using Gson but then I began to wonder if it was the right choice.</p>
  
  <ol>
  <li>Is it possible to serialize and deserialize JSON to/from model
  object using @RestController annotation? How does it works? I only find examples of how to create a rest service, in this sense</li>
  <li>What is the best practice in this case? RestController? Gson? Jackson? Other?</li>
  </ol>
  
  <p><strong>N.B.</strong> My JsonResponse object has a generic field because the responses are all the same except that for a value. An example:</p>
  
  <p><strong>AccountRequest.class</strong></p>
  
  <pre><code>public class AccountRequest() {
      private String jsonrpc;
      private String method;
      private Map&lt;String, Object&gt; params;
      private int id;
  
      //constructor, getters and setters
  }
  </code></pre>
  
  <p><strong>JsonResponse.class</strong></p>
  
  <pre><code>public class JsonResponse&lt;T&gt; {
      private String jsonrpc;
      private T result;
      private String id;
  
      //constructor, getters and setters
  }
  </code></pre>
  
  <p><strong>LoginResult.class</strong></p>
  
  <pre><code>public class LoginResult {
      private String token;
      private String product;
      private String error;
      private Date lastLoginDate;
  
      //constructor, getters and setters
  }
  </code></pre>
  
  <p><strong>MyController.class</strong></p>
  
  <pre><code>@Controller
  public class LoginController {
      String response;
      [...]
      @RequestMapping(value = "/login", method = RequestMethod.POST)
      public ModelAndView doLogin(@ModelAttribute("user") User user) {
          [...]
          //???
          [...]
      }
  }
  </code></pre>
  
  <p>I open connection, send request and save the response in response String in MyController class. User parameter is annotated with @ModelAttribute to retrive the User object created by the showLoginForm() method. </p>
  
  <p>Using Gson I know how to serialize request from my model object:</p>
  
  <pre><code>AccountRequest accountRequest = new AccountRequest();
  accountRequest.setJsonrpc("2.0");
  accountRequest.setMethod("method");
  accountRequest.setParams(params);
  accountRequest.setId(1);
  
  String gson = new Gson().toJson(accountRequest);
  JSONObject accountRequestJson = new JSONObject(gson);
  </code></pre>
  
  <p>And how to deserialize response in my model object:</p>
  
  <pre><code>Type jsonType = new TypeToken&lt;JsonResponse&lt;LoginResult&gt;&gt;() {}.getType();
  JsonResponse&lt;LoginResult&gt; jsonResponse = new Gson().fromJson(response, jsonType);
  </code></pre>
  
  <p>The response is now saved into jsonResponse object, and I can put it in my ModelAndView.</p>
  
  <p>Now, how can I deserialize it in my model object using @RestController and Jackson? Can you confirm it's the better method to do that? Can you post an example?</p>
  
  <p>I hope it's not a silly question.
  Thanks in advance!</p>