rest - Returning JSON from RESTful Java server code? -


i've inherited web project contractor started. , coworkers unfamiliar technology used, , have number of questions. can tell, appears sort of restful java server code, understanding there lots of different types of java restful services. 1 this? specific questions:

1) can read more (particularly introductory information) specific service?

2) code creates , returns json through kind of "magic"... merely return model class (code below) has getter , setter methods fields, , it's automagically converted json. i'd learn more how done automagically.

3) have code creates json. need return using framework. if already have json, how return that? tried this:

string testjson = "{\"menu\": {\"id\": \"file\", \"value\": \"hello there\"}}"; return testjson;

instead of returning model object getters/setters, returns literal text string, not json. there way return actual json that's json string, , have sent json?

you don't have able answer of questions above. any/all pointers in helpful direction appreciated!

code

first, view controller returns json:

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 {         // method return list of subscription_tier table entries active     @get     @produces({ mediatype.application_json })     public string/*arraylist<subscriptiontiermodel>*/ getsubscriptiontiers(@queryparam("includeactiveonly") boolean includeactiveonly)     {                mysqlconnection mysql = mysqlconnection.getconnection();         arraylist<subscriptiontiermodel> subscriptiontierarray = new arraylist<subscriptiontiermodel>();         string querystring;          if (includeactiveonly)             querystring = "select * subscription_tier active=1";         else             querystring = "select * subscription_tier";          list<map<string, object>> resultlist = mysql.query(querystring, null);          (map<string, object> subscriptionrow : resultlist)             subscriptiontierarray.add( new subscriptiontiermodel(subscriptionrow) );      //  string testjson = "{\"menu\": {\"id\": \"file\", \"value\": \"hello there\"}}";     //  return testjson;          return subscriptiontierarray;     } } 

next, model code above returns:

package com.aimcloud.models; // note 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<string, object> 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<string> 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<string>();         this.fields.add("id");     }      public void initfromdbresult(list<map<string, object>> result) {         if (result.size() >= 1)         {             map<string, object> userrow = result.get(0);             this.initfrommap(userrow);              if (result.size() > 1)             {             thread.dumpstack();             }         }          else          {             throw new webapplicationexception(serverutils.generateresponse(response.status.not_found, "resource not found"));         }     }      protected void initfrommap(map<string, object> map) {         (map.entry<string, object> entry : map.entryset()) {             object value = entry.getvalue();             // loggingutils.log(entry.getkey() + " " + entry.getvalue().tostring());             if (value != null && this.fields.contains(entry.getkey())) {                 this.setfield(entry.getkey(), value);             }         }     }  .... 

1) can read more (particularly introductory information) specific service?

this restful service uses basic jax-rs annotations build service. suggest looking @ tutorial "rest using jersey" or "rest using cxf".

2) code creates , returns json through kind of "magic"...

the restful framework used takes care of this. @produces({ mediatype.application_json }) annotation indicates framework conversion.this defined somewhere in configuration. check spring config files if using spring define beans. mapper or provider defined converts object json.

3) have code creates json. need return using framework. if have json, how return that? tried this:

if have json return json method. remember still have @produces({ mediatype.application_json }) annotation on method.

but returns literal text string, not json

a json string. see in response, unless deserialize object.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -