rest - java client consuming API -
i trying use mail gun api getting bounce mail data.
public static clientresponse getbounce() { client client = new client(); client.addfilter(new httpbasicauthfilter("api", "key-xxxxxxxxxxxxxxxxxxxxx")); webresource webresource = client.resource("https://api.mailgun.net/v3/xxxxxxxxxxxx.mailgun.org/" + "bounces/foo@bar.com"); return webresource.get(clientresponse.class);}
it works fine api call ok not able convert clientresponse in appropriate type in cae emailerror . actual response server {"address":"a@gmail.com","code":"550","error":"550 5.2.1 email account tried reach disabled. lq5si9613879igb.63 - gsmtp","created_at":"tue, 18 aug 2015 12:23:35 utc"}
i created pojo map response
@jsonautodetect(gettervisibility = visibility.none, fieldvisibility = visibility.any) @jsonserialize(include = inclusion.non_null) public class emailerror { private string address; private string error; private string created_at; //geters... //setters.. }
and try map clientresponse emailerror type.
clientresponse clientresponse = getbounce(email); emailerror error = response.getentity(new generictype<emailerror>() { });
where clientresponse object return method getbounce()
it throws exception com.sun.jersey.api.client.clienthandlerexception: message body reader java class com.che.rt.businessservices.emailerror, , java type class com.che.rt.businessservices.emailerror, , mime media type application/json;charset=utf-8 not found
any guess missing.
you seem have jackson dependency (as you're using annotations) not enough jersey. jersey uses messagebodyreader
s deserialize request/response bodies. need 1 can handle json. jackson implements jax-rs reader , writer. dependency need jackson provider, not primary jackson libraries.
for jersey can add dependency
<dependency> <groupid>com.sun.jersey</groupid> <artifactid>jersey-json</artifactid> <version>1.19</version> </dependency>
this pull in jackson 1.9.2, can rid of other jackson dependencies have, version don't conflict. need register jackson provider jersey client. can do
clientconfig config = new defaultclientconfig(); config.getproperties().put(jsonconfiguration.feature_pojo_mapping, true); config.getsingletons().add(new jacksonjsonprovider()); client client = client.create(config);
note above uses jackson 1.9.2. if want use newer 2.x jackson, instead of above dependency, use one
<dependency> <groupid>com.fasterxml.jackson.jaxrs</groupid> <artifactid>jackson-jaxrs-json-provider</artifactid> <!-- or whatever [2.2,) version want use --> <version>2.5.0</version> </dependency>
then configuration should different
clientconfig config = new defaultclientconfig(); config.getsingletons().add(new jacksonjsonprovider()); client client = client.create(config);
Comments
Post a Comment