2015-10-15 194 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 –