title: JERSEY - returning javax.ws.rs.core.Response as a JSON object with either error:... or token:
body: <p><strong>EDIT</strong>: Is there any way to return a simple object that has either:</p>
  
  <ul>
  <li><strong>token</strong></li>
  <li><strong>error</strong></li>
  <li><strong>error</strong> <em>and</em> <strong>details</strong></li>
  </ul>
  
  <p>...without having to create new Objects.
  <strong>what's the most efficient and understandable way to do this?</strong></p>
  
  <p>I am trying to make a login service that <code>@Consumes(MediaType.APPLICATION_JSON)</code> (containing login credentials: username, password, consumer(boolean)).</p>
  
  <p>If the credentials are valid I want to return a token, for future on the front end, and a <em>RESPONSE CODE</em> <strong>OK</strong>/<strong>200</strong>; something like this JSON object:</p>
  
  <pre><code>{"token":"aAKGKas211"}
  </code></pre>
  
  <p>Otherwise I have specific errors for every case and I want to return a different <em>RESPONSE CODE</em>, which would look something like this:</p>
  
  <pre><code>{"error":"Invalid username or password"}
  </code></pre>
  
  <p>I tried to add a String error (="Invalid username or password") to the Response.Status.NOT_ACCEPTABLE).entity(error), but I'm getting the string as a string not as a JSON object..</p>
  
  <p>this is how my code looks like:</p>
  
  <pre><code>@POST
  @Path("/login")
  @Produces(MediaType.APPLICATION_JSON)
  @Consumes(MediaType.APPLICATION_JSON)
  public Response login(@Context HttpServletRequest req, LoginInfo loginInfo) {
      CouponClientFacade facade = null;
      String token = null, error = null;
  
      // validate the REQUESTer's token, if he is already logged in return ERROR RESPONSE.
      System.out.println(loginInfo);
      System.out.println(req.getSession().getId());
      if (loginInfo == null)
          return Response.ok("Test").build();
      if (validateCredentials(loginInfo))
          return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Invalid username or password").build();
  
  
      try { // Try to login into administrator
          facade = ClientType.ADMIN.login(loginInfo.getUsername(), loginInfo.getPassword());
      } catch (Exception E) { // Couldn't login into administrator
          try {
              if (loginInfo.isCustomer()) // Try to login into CUSTOMER account
                  facade = ClientType.CUSTOMER.login(loginInfo.getUsername(), loginInfo.getPassword());
              else // Try to login into COMPANY account
                  facade = ClientType.COMPANY.login(loginInfo.getUsername(), loginInfo.getPassword());
  
          } catch (BadUsernamePassword e) {
              return Response.status(Response.Status.FORBIDDEN).entity(error=e.getMessage()).build();
          } catch (UnexpectedError e) {
              return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error=e.getMessage()).build();
          }
      }
      if (facade != null) {
          token = SessionManager.generateToken(req.getSession().getId(), facade);
          return Response.ok(token).build();
      } else return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error="111").build();
  }
  </code></pre>
  
  <p>edit:</p>