java - Spring mvc restful service and client -
hi friends service code in server side,
@requestmapping(value = "/pack/{id}", method = requestmethod.get, headers="accept=application/json", produces = "application/json") public @responsebody string getpackbyid(@pathvariable("id") int packageid) { system.out.println("in restful service menthod ****************************"); vspackages vspackages = vspackages.findvspackages(integer.valueof(packageid)); return vspackages.customtojson(); }
its working, returns json object when uri called (http://thevenueplanner.com/pack/480)
i wrote java client access return json object uri
client code here
string base_uri = "http://thevenueplanner.com/";
public void givenconsumingjson_whenreadingthefoo_thencorrect() { string uri = base_uri + "pack/{id}"; jsonarray arr = null; resttemplate resttemplate = new resttemplate(); resttemplate.setmessageconverters(getmessageconverters()); httpheaders headers = new httpheaders(); headers.setaccept(arrays.aslist(mediatype.application_json)); httpentity<string> entity = new httpentity<string>(headers); responseentity<jsonobject> response = resttemplate.exchange(uri, httpmethod.get, entity, jsonobject.class, "480"); jsonobject resource = resttemplate.getforobject(uri, jsonobject.class, "480"); system.out.println("response: "+response.tostring()); system.out.println("resource: "+resource.tostring()); } private list<httpmessageconverter<?>> getmessageconverters() { objectmapper mapper = new objectmapper(); mapper.configure(serializationfeature.wrap_root_value, true); mapper.configure(deserializationfeature.fail_on_unknown_properties, false); mappingjackson2httpmessageconverter messageconverter = new mappingjackson2httpmessageconverter(); messageconverter.setobjectmapper(mapper); list<httpmessageconverter<?>> converters = new arraylist<httpmessageconverter<?>>(); converters.add(messageconverter); return converters; }
the success message is:
response: <200 ok,{},{server=[apache-coyote/1.1], content-type=[application/json;charset=utf-8], content-length=[5189], date=[tue, 03 jun 2014 08:54:50 gmt]}> resource: {}
it wont give json object, tells content length in success message,
how json object uri client
Comments
Post a Comment