我正在尝试使用邮件枪API获取退回邮件数据。
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);}
它运行良好的API调用是可以的,但我无法在我的cae EmailError中以适当的类型转换ClientResponse。
服务器的实际响应为{"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"}
我创建了POJO来映射响应
@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..
}
尝试将ClientResponse映射到EmailError类型。
ClientResponse clientResponse = getBounce(email);
EmailError error = response.getEntity(new GenericType<EmailError>() {
});
其中clientResponse是按方法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
猜测我失踪的地方。
答案 0 :(得分:1)
你似乎有杰克逊的依赖(因为你正在使用它的注释),但这对泽西岛来说还不够。 Jersey使用MessageBodyReader
来反序列化请求/响应主体。所以你需要一个可以处理JSON的东西。杰克逊确实实现了JAX-RS读写器。因此,您需要的依赖是杰克逊提供商,而不仅仅是杰克逊的主要图书馆。
对于Jersey,您可以添加此依赖项
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.19</version>
</dependency>
这将拉入杰克逊1.9.2,所以你可以摆脱你拥有的其他杰克逊依赖,只是这样版本不冲突。然后,您需要向Jersey客户注册Jackson提供商。为此你可以做到
ClientConfig config = new DefaultClientConfig();
config.getProperties().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
config.getSingletons().add(new JacksonJsonProvider());
Client client = Client.create(config);
注意以上使用Jackson 1.9.2。如果你想使用更新的2.x Jackson,那么使用这个
而不是上面的依赖<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>
然后配置也应该不同
ClientConfig config = new DefaultClientConfig();
config.getSingletons().add(new JacksonJsonProvider());
Client client = Client.create(config);