2015-10-15 196 views
0

我正在尝试使用邮件枪API获取反弹邮件数据。Java客户端消费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/[email protected]"); 
    return webResource.get(ClientResponse.class);} 

它正常工作的API调用就可以了,但我不能ClientResponse转换成合适的类型在我的CAE EmailError。从服务器 实际响应{"address":"[email protected]","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

显示您的依赖。 –

+0

\t \t \t com.sun.jersey \t \t \t 球衣的客户端 \t \t \t 1.19 \t \t

回答

1

你似乎有杰克逊的依赖(因为你使用它的注释),但这对泽西来说还不够。 Jersey使用MessageBodyReader来反序列化请求/响应主体。所以你需要一个可以处理JSON的东西。杰克逊确实实现了JAX-RS读写器。所以你需要的依赖是杰克逊的提供者,而不仅仅是杰克逊的主要图书馆。

泽西岛你可以添加这种依赖性

<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); 

注意上面的使用杰克逊1.9.2。如果你想用一个新的2.x的杰克逊,然后代替上述的依赖,用这一个

<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); 
+0

同样的问题com.sun.jersey.api.client.ClientResponse getEntity 严重:用于Java类com.cheasyy.cofinding.businessservices.EmailError和Java类com.che.rt.businessservice的消息正文阅读器s.EmailError和MIME媒体类型application/json;未找到charset = utf-8 2015年10月15日下午3:07:40 com.sun.jersey.api.client.ClientResponse getEntity SEVERE:已注册的邮件正文与MIME媒体类型兼容的阅读器是: application/json; charset = utf-8 - > com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider $ App –

+0

适合我。你为什么使用'GenericType'?没有什么通用的'EmailError' –

+0

是的,这不是必需的我只是在尝试它。它的工作完美webResource.get(ClientResponse.class);}但不能转换为EmailError –