I am trying to use mail gun API for 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 is ok but I am not able to convert ClientResponse in appropriate type in my cae EmailError .
Actual response from server is {"address":"a@gmail.com","code":"550","error":"550 5.2.1 The email account that you tried to reach is disabled. lq5si9613879igb.63 - gsmtp","created_at":"Tue, 18 Aug 2015 12:23:35 UTC"}
I created POJO to 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 to map ClientResponse into EmailError type.
ClientResponse clientResponse = getBounce(email);
EmailError error = response.getEntity(new GenericType<EmailError>() {
});
where clientResponse is object return by method GetBounce()
It throws an Exception com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class com.che.rt.businessservices.EmailError, and Java type class com.che.rt.businessservices.EmailError, and MIME media type application/json;charset=utf-8 was not found
Any guess where I am missing.
You seem to have the Jackson dependency (as you're using its annotations) but that is not enough for Jersey. Jersey uses MessageBodyReader
s to deserialize request/response bodies. So you need one that can handle JSON. Jackson does implements a JAX-RS reader and writer. So the dependency you need is the Jackson provider, not just the primary Jackson libraries.
For Jersey you can add this dependency
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.19</version>
</dependency>
This will pull in Jackson 1.9.2, so you can get rid of the other Jackson dependencies you have, just so version don't conflict. Then you need to register the Jackson provider with the Jersey client. For that you 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 the above uses Jackson 1.9.2. If you want to use a newer 2.x Jackson, then instead of the above dependency, use this one
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<!-- or whatever [2.2,) version you want to use -->
<version>2.5.0</version>
</dependency>
Then configuration should also be different
ClientConfig config = new DefaultClientConfig();
config.getSingletons().add(new JacksonJsonProvider());
Client client = Client.create(config);