title: Returning JSON from RESTful Java server code?
body: <p>I've inherited a web project that a contractor started. I and my coworkers are unfamiliar with the technology used, and have a number of questions. From what we can tell, this appears to be some sort of RESTful Java server code, but my understanding is there are lots of different types of Java RESTful services. Which one is this? Specific questions:</p>
  
  <p>1) Where can we read more (particularly introductory information) about this specific service?</p>
  
  <p>2) The code creates and returns a JSON through some kind of "magic"... I merely return a model class (code below) that has getter and setter methods for its fields, and it's automagically converted into a JSON. I'd like to learn more about how this is done automagically.</p>
  
  <p>3) We already have some code that creates a JSON. We need to return this using this framework. If I <em>already</em> have a JSON, how do I return that? I tried something like this:</p>
  
  <p>String testJSON = "{\"menu\": {\"id\": \"file\", \"value\": \"Hello there\"}}";
  return testJSON;</p>
  
  <p>instead of returning a model object with getters/setters, but this returns a literal text string, not a JSON. Is there a way to return an actual JSON that's already a JSON string, and have it be sent as a JSON?</p>
  
  <p>You don't have to be able to answer all of the questions above. Any/all pointers in a helpful direction appreciated!</p>
  
  <p><strong>CODE</strong></p>
  
  <p>First, the view controller that returns the JSON:</p>
  
  <pre><code>package com.aimcloud.server;
  import com.aimcloud.util.MySqlConnection;
  import javax.ws.rs.GET;
  import javax.ws.rs.PUT;
  import javax.ws.rs.POST;
  import javax.ws.rs.Path;
  import javax.ws.rs.PathParam;
  import javax.ws.rs.QueryParam;
  import javax.ws.rs.FormParam;
  import javax.ws.rs.HeaderParam;
  import javax.ws.rs.Produces;
  import javax.ws.rs.WebApplicationException;
  import javax.ws.rs.core.MediaType;
  import java.io.File;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import com.aimcloud.models.SubscriptionTierModel;
  
  @Path("subscription_tier")
  public class SubscriptionTierController
  {
          // this method will return a list of subscription_tier table entries that are currently active
      @GET
      @Produces({ MediaType.APPLICATION_JSON })
      public String/*ArrayList&lt;SubscriptionTierModel&gt;*/ getSubscriptionTiers(@QueryParam("includeActiveOnly") Boolean includeActiveOnly)
      {       
          MySqlConnection mysql = MySqlConnection.getConnection();
          ArrayList&lt;SubscriptionTierModel&gt; subscriptionTierArray = new ArrayList&lt;SubscriptionTierModel&gt;();
          String queryString;
  
          if (includeActiveOnly)
              queryString = "SELECT * FROM subscription_tier WHERE active=1";
          else
              queryString = "SELECT * FROM subscription_tier";
  
          List&lt;Map&lt;String, Object&gt;&gt; resultList = mysql.query(queryString, null);
  
          for (Map&lt;String, Object&gt; subscriptionRow : resultList)
              subscriptionTierArray.add( new SubscriptionTierModel(subscriptionRow) );
  
      //  String testJSON = "{\"menu\": {\"id\": \"file\", \"value\": \"Hello there\"}}";
      //  return testJSON;
  
          return subscriptionTierArray;
      }
  }
  </code></pre>
  
  <p>Next, the model the code above returns:</p>
  
  <pre><code>package com.aimcloud.models;
  // NOTE this does NOT import Globals
  import java.sql.Types;
  import java.util.Arrays;
  import java.util.Calendar;
  import java.util.Date;
  import java.util.List;
  import java.util.Map;
  
  import org.json.JSONObject;
  import com.aimcloud.util.LoggingUtils;
  
  public class SubscriptionTierModel extends ModelPrototype
  {
      private String name;
      private Integer num_studies;
      private Integer cost_viewing;
      private Integer cost_processing;
      private Integer active;
  
      protected void setupFields()
      {
          this.fields.add("name");
          this.fields.add("num_studies");
          this.fields.add("cost_viewing");
          this.fields.add("cost_processing");
          this.fields.add("active");
      }
  
      public SubscriptionTierModel()
      {
          super("subscription");
          this.setupFields();
      }
  
      public SubscriptionTierModel(Map&lt;String, Object&gt; map)
      {
          super("subscription");
          this.setupFields();
          this.initFromMap(map);
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getName() {
          return this.name;
      }
  
      public void setNum_Studies(Integer num_studies) {
          this.num_studies = num_studies;
      }
  
      public Integer getNum_studies() {
          return this.num_studies;
      }
  
      public void setCost_viewing(Integer cost_viewing) {
          this.cost_viewing = cost_viewing;
      }
  
      public Integer getCost_viewing() {
          return this.cost_viewing;
      }
  
      public void setCost_processing(Integer cost_processing) {
          this.cost_processing = cost_processing;
      }
  
      public Integer getCost_processing() {
          return this.cost_processing;
      }
  
      public void setActive(Integer active) {
          this.active = active;
      }
  
      public Integer getActive() {
          return this.active;
      }
  }
  
  
  public abstract class ModelPrototype {
      protected MySqlConnection mysql;
  
      protected ArrayList&lt;String&gt; fields;
      protected String table;
      protected Integer id = null;
  
      public Integer getId() {
          return this.id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      abstract protected void setupFields();
  
      public ModelPrototype() {
          mysql = MySqlConnection.getConnection();
          this.fields = new ArrayList&lt;String&gt;();
          this.fields.add("id");
      }
  
      public void initFromDbResult(List&lt;Map&lt;String, Object&gt;&gt; result) {
          if (result.size() &gt;= 1)
          {
              Map&lt;String, Object&gt; userRow = result.get(0);
              this.initFromMap(userRow);
  
              if (result.size() &gt; 1)
              {
              Thread.dumpStack();
              }
          } 
          else 
          {
              throw new WebApplicationException(ServerUtils.generateResponse(Response.Status.NOT_FOUND, "resource not found"));
          }
      }
  
      protected void initFromMap(Map&lt;String, Object&gt; map) {
          for (Map.Entry&lt;String, Object&gt; entry : map.entrySet()) {
              Object value = entry.getValue();
              // LoggingUtils.log(entry.getKey() + " " + entry.getValue().toString());
              if (value != null &amp;&amp; this.fields.contains(entry.getKey())) {
                  this.setField(entry.getKey(), value);
              }
          }
      }
  
  ....
  </code></pre>