java - Parse a JSON response as an object -
good evening,
newbie in web services , want write simple client gets responses rest calls.
downloaded , added jersey-bundle-1.17.1.jar
build path , found working piece of code makes rest call url , returns response string
.
import javax.ws.rs.core.mediatype; import com.sun.jersey.api.client.client; import com.sun.jersey.api.client.clientresponse; import com.sun.jersey.api.client.webresource; public class testjerseyclient { public static void main(string[] args) { try { client client = client.create(); webresource webresource = client .resource("http://path/to/service"); clientresponse response = webresource .accept(mediatype.application_json) .get(clientresponse.class); if (response.getstatus() != 200) throw new runtimeexception("failed : http error code : " + response.getstatus()); string output = response.getentity(string.class); system.out.println("output server .... \n"); system.out.println(output); } catch (exception e) { e.printstacktrace(); } } }
but don't want string manipulations parts of response im interested in.
i prefer more structured approach, getting object
instead of string
(a json
object, map<k,v>
object, etc...)
try#1 -
map<k,v>
sep 15, 2013 2:46:13 com.sun.jersey.api.client.clientresponse getentity severe: message body reader java class java.util.map, , java type java.util.map<java.lang.string, java.lang.object>, , mime media type application/json; charset=utf-8 not found sep 15, 2013 2:46:13 com.sun.jersey.api.client.clientresponse getentity severe: registered message body readers compatible mime media type are: application/json; charset=utf-8 -> com.sun.jersey.json.impl.provider.entity.jsonjaxbelementprovider$app com.sun.jersey.json.impl.provider.entity.jsonrootelementprovider$app com.sun.jersey.json.impl.provider.entity.jsonlistelementprovider$app */* -> com.sun.jersey.core.impl.provider.entity.formprovider com.sun.jersey.core.impl.provider.entity.stringprovider com.sun.jersey.core.impl.provider.entity.bytearrayprovider com.sun.jersey.core.impl.provider.entity.fileprovider com.sun.jersey.core.impl.provider.entity.inputstreamprovider com.sun.jersey.core.impl.provider.entity.datasourceprovider com.sun.jersey.core.impl.provider.entity.xmljaxbelementprovider$general com.sun.jersey.core.impl.provider.entity.readerprovider com.sun.jersey.core.impl.provider.entity.documentprovider com.sun.jersey.core.impl.provider.entity.sourceprovider$streamsourcereader com.sun.jersey.core.impl.provider.entity.sourceprovider$saxsourcereader com.sun.jersey.core.impl.provider.entity.sourceprovider$domsourcereader com.sun.jersey.json.impl.provider.entity.jsonjaxbelementprovider$general com.sun.jersey.core.impl.provider.entity.xmlrootelementprovider$general com.sun.jersey.core.impl.provider.entity.xmllistelementprovider$general com.sun.jersey.core.impl.provider.entity.xmlrootobjectprovider$general com.sun.jersey.core.impl.provider.entity.entityholderreader com.sun.jersey.json.impl.provider.entity.jsonrootelementprovider$general com.sun.jersey.json.impl.provider.entity.jsonlistelementprovider$general com.sun.jersey.api.client.clienthandlerexception: message body reader java class java.util.map, , java type java.util.map<java.lang.string, java.lang.object>, , mime media type application/json; charset=utf-8 not found @ com.sun.jersey.api.client.clientresponse.getentity(clientresponse.java:561) @ com.sun.jersey.api.client.clientresponse.getentity(clientresponse.java:535) @ com.sun.jersey.api.client.webresource.handle(webresource.java:696) @ com.sun.jersey.api.client.webresource.access$300(webresource.java:74) @ com.sun.jersey.api.client.webresource$builder.get(webresource.java:512) @ rest.testjerseyclientadvanced.main(testjerseyclientadvanced.java:36)
you can use genson library.
// register genson in jersey client clientconfig cfg = new defaultclientconfig(gensonjsonconverter.class); client client = client.create(cfg); webresource webresource = client.resource("http://path/to/service"); // can map pojo, no need have string or map somepojo pojo = webresource .accept(mediatype.application_json) .get(somepojo.class);
Comments
Post a Comment