web services - Capture JSON from representation and return String value -
i have working webservice shown below return double value in json format e.g. {"pmc":17.34}
@override @post("json") public representation post(representation entity) throws resourceexception { jsonobjectbuilder response = json.createobjectbuilder(); try { string json = entity.gettext(); // json input client map<string, object> map = jsonutils.tomap(json); // convert input map double result = matrix.calculatepmc(map); // calculate pmc value response.add("pmc", result); } catch (ioexception e) { logger.error(this.getclass() + " - ioexception - " + e); getresponse().setstatus(status.server_error_internal); } return new stringrepresentation(response.build().tostring()); }
now, change program return double value, i.e. 17.34
, modified program following:
@post public double post(response response) { double result = 0; try { string json = response.getentity().gettext(); //get json input client map<string, object> map = jsonutils.tomap(json); // convert input map result = matrix.calculatepmc(map); // calculate pmc value } catch (ioexception e) { logger.error(this.getclass() + " - ioexception - " + e); getresponse().setstatus(status.server_error_internal); } return result; }
when run 415 unsupported media type
error. missing?
misread program requirements, should return string value (of double) instead. following final working program:
@post public string post(string json) { double result = 0; map<string, object> map = jsonutils.tomap(json); // convert json input map result = matrix.calculatepmc(map); // calculate pmc value return string.valueof(result); }
Comments
Post a Comment