2012-04-13 82 views
2

我休息web服務(含球衣),返回JSON的列表請求休息JSON的服務,如果我把它直接返回正是這一點:產生javax.xml.bind.UnmarshalException當球衣客戶

[{"success":false,"uri":"foo:22","message":"Unknown host : foo"},{"success":true,"uri":"localhost:8082","message":null}] 

通過這個片段:

@GET 
@Path("/opening/") 
public List<OpeningResult> testOpenings(@QueryParam("uri") List<String> uris) { 
    LOG.debug("testOpenings request uris :[" + uris + "]"); 
    List<OpeningResult> openingResults = infoService.testOpenings(uris); 
    return openingResults; 
} 

這是一個POJO集合看起來是這樣的:

@XmlRootElement(name = "OpeningResult") 
public class OpeningResult { 

attributes 
... 
getter/setter 

} 

這個POJO的是通過服務器和客戶端之間的通用jar進行共享。

我調用Web服務在這個片段:

Client client = Client.create(); 
WebResource resource = client.resource("http://localhost:8080/scheduler/rest/opening"); 
MultivaluedMap<String, String> params = new MultivaluedMapImpl(); 
for (String uri : uris) { 
    params.add("uri", uri); 
} 
List<OpeningResult> results = newArrayList(resource.queryParams(params).get(OpeningResult[].class)); 

我添加服務器端的一些痕跡,我看到我的其他服務被稱爲與良好的參數,在客戶端buth,我有這個錯誤:

Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"success"). Expected elements are <{}OpeningResult> 

我不知道它來自哪裏?

+0

你可以發佈發佈Pojo的服務器端代碼嗎? – 2012-04-13 13:05:05

+0

我編輯我的消息與片段。 – Antoine 2012-04-17 09:48:45

回答

0

查看Jersey Client side doc on using JSON。它看起來像你至少錯過了註釋:

@Produces("application/json") 

但你也可能會錯過客戶端和服務器端的POJO映射功能過濾器。這些似乎都是很小的配置更改。

+0

Nop我已經把這個註解放在類上,而不是每種方法上。 pojo被充分註釋。 – Antoine 2012-04-17 19:21:45

+0

POJO映射功能並非特定於您的班級。它們是您爲Web應用程序和ClientConfig類配置的過濾器。這就是我的意思是「用於客戶端和服務器端的功能過濾器」。 http://jersey.java.net/nonav/documentation/latest/json.html#json.jaxb.approach.section – 2012-04-17 20:28:34

1

修改代碼來設置你的客戶是這樣的:

ClientConfig clientConfig = new DefaultClientConfig(); 
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true); 
Client client = Client.create(clientConfig); 

我有完全相同的問題,直到this question and its answers我指出了正確的方向。

這種情況是由默認的jersey-json module用於JSON序列化和從JSON序列化,JSON不能正確處理某些JSON結構。

您可以將FEATURE_POJO_MAPPING標誌設置爲使用Jackson庫的JacksonJsonProvider替代JSON序列化。