title: Spring java ResponseEntity<T> with multiple parameter types
body: <p>I'm working with java and Spring MVC, In the first version of the app I was response with a <code>ResponseEntity&lt;String&gt;</code> and where I haved and error I returned something like <code>return new ResponseEntity&lt;String&gt;(httpErrors.toString(), responseHeaders, HttpStatus.BAD_REQUEST);</code> and when all were right something like <code>return new ResponseEntity&lt;String&gt;(loginResponse.toString(), responseHeaders, HttpStatus.OK);</code>. But now I believe theres is a better way to do it, without using the <code>toString()</code> method, returning the specific object according to the case like this:</p>
  
  <pre><code>@RestController
  @RequestMapping("/user")
  public class LoginController {
  
      /** The login service to validate the user. */
      @Autowired
      LoginService loginService;
  
      @RequestMapping(value = "/login", method = RequestMethod.POST)
      public @ResponseBody ResponseEntity&lt;?&gt; validate(@RequestBody final UserLog login) {
  
          WebUser webUser = loginService.getUserDetails(login.getLogin(), login.getPassword());
          HttpHeaders responseHeaders = new HttpHeaders();
          responseHeaders.setContentType(MediaType.APPLICATION_JSON);
  
          if (webUser == null) {
              HttpErrors httpErrors = new HttpErrors(ApiCommonResources.ERROR_402, "error" + "." + ApiCommonResources.ERROR_402, ApiCommonResources.ERROR_402_TEXT);
              return new ResponseEntity&lt;HttpErrors&gt;(httpErrors, responseHeaders, HttpStatus.BAD_REQUEST);
          }
  
          List&lt;Account&gt; userAccounts = loginService.getMerchantAccounts(webUser.getMerchantId());
  
          // Json Web Token builder
          token = "b7d22951486d713f92221bb987347777";
  
          LoginResponse loginResponse = new LoginResponse(ApiCommonResources.SUCCESS_REQUEST_CODE, token);
  
          return new ResponseEntity&lt;LoginResponse&gt;(loginResponse, responseHeaders, HttpStatus.OK);
  
      }
  
  }
  </code></pre>
  
  <p>The question is how can I create a class that can wraps the <code>LoginResponse</code> as well as <code>HttpErrors</code>object types and send it in <code>?</code> as the returning object in ResponseEntity:</p>
  
  <p>LoginResponse class:</p>
  
  <pre><code>public class LoginResponse{
  
      public LoginResponse(Integer statusCode, String token){
          this.setStatusCode(statusCode);
          this.setToken(token);
      }
  
      private String token;
      private Integer statusCode;
  
      public String getToken() {
          return token;
      }
  
      public void setToken(String token) {
          this.token = token;
      }
  
      public Integer getStatusCode() {
          return statusCode;
      }
  
      public void setStatusCode(Integer statusCode) {
          this.statusCode = statusCode;
      }
  
      @Override
      public String toString() {
  
          StringBuilder jsonResponse = new StringBuilder();
  
          jsonResponse.append("{");
          jsonResponse.append("\"statusCode\":");
          jsonResponse.append("\"" + statusCode + "\",");
          jsonResponse.append("\"token\":");
          jsonResponse.append("\"" + token + "\"");
          jsonResponse.append("}");
  
          return jsonResponse.toString();
      }
  
  }
  </code></pre>
  
  <p>And HttpErrors class:</p>
  
  <pre><code>public class HttpErrors {
  
      public HttpErrors(){
      }
  
      public HttpErrors(String errorCode, String errorKey, String errorMessage) {
          super();
          this.errorCode = errorCode;
          this.errorKey = errorKey;
          this.errorMessage = errorMessage;
      }
  
      private String errorCode;
      private String errorKey;
      private String errorMessage;
  
      public String getErrorCode() {
          return errorCode;
      }
  
      public void setErrorCode(String errorCode) {
          this.errorCode = errorCode;
      }
  
      public String getErrorKey() {
          return errorKey;
      }
  
      public void setErrorKey(String errorKey) {
          this.errorKey = errorKey;
      }
  
      public String getErrorMessage() {
          return errorMessage;
      }
  
      public void setErrorMessage(String errorMessage) {
          this.errorMessage = errorMessage;
      }
  
      @Override
      public String toString() {
  
          StringBuilder jsonError = new StringBuilder();
  
          jsonError.append("{");
          jsonError.append("\"errorCode\":");
          jsonError.append("\"" + errorCode + "\",");
          jsonError.append("\"errorKey\":");
          jsonError.append("\"" + errorKey + "\",");
          jsonError.append("\"errorMessage\":");
          jsonError.append("\"" + errorMessage + "\"");
          jsonError.append("}");
  
          return jsonError.toString();
      }
  
  }
  </code></pre>