2012-03-28 28 views
2

我嘗試輸出documemts的一些列表從資源:編組名單<T>與新澤西州和JAXB

@GET 
@Produces({MediaType.APPLICATION_XML}) 
public Response getDocuments(@QueryParam("provider") String provider) { 
    List<Document> documents = service.getDocuments(provider); 
    return Response.ok(
     new GenericEntity<List<Document>>(
     new ArrayList<Document>(documents)) {}) 
    .build(); 
} 

文檔類不與@XmlElement註釋(我真的不喜歡我的註釋對象很好用這樣的低層次的東西...),但我有一個註冊商爲它:

@Service 
@Provider 
@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.WILDCARD}) 
@Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.WILDCARD}) 
public class JaxRsDocumentSerializer extends 
    AbstractMessageReaderWriterProvider<Document> { 

當我打電話使用的客戶端與下面的代碼此資源:

return getResource().path("/documents") 
    .queryParam("provider", provider) 
    .accept(MediaType.APPLICATION_XML).get(new GenericType<List<Document>>() {}); 

我得到了可怕的例外:

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: 
A message body writer for Java class java.util.ArrayList, and Java type 
java.util.List<com.polyspot.model.core.Document>, 
and MIME media type application/xml was not found 

我不明白什麼是錯這裏我也跟着代碼,我發現其他地方,包括SO。

非常感謝。

+0

你能否提供任何關於class Document的信息?是否正確註釋? – 2012-03-28 13:06:24

+0

對不起。是的。我們一直在使用Jersey對「年齡」進行編組/解組文檔實例。 – insitu 2012-03-28 15:46:57

+0

Oups。上面的評論是不正確的,我們不註釋對象,但使用自定義提供程序。我更新了這個問題來反映這一點。 – insitu 2012-03-28 15:52:42

回答

2

也許你的Document類需要@XmlRootElement annonation。或者,如果您的Jersey版本> = 1.2,則可以嘗試使用JResposne而不是Response。這可以避免使用GenericEntity

+0

+1 Document類需要使用@XmlRootElement進行註釋。 – 2012-03-28 14:20:52

+0

謝謝,我添加了一條評論並更新了我的問題,以防萬一。我寧願堅持純粹的JAX-RS,因爲我希望儘可能多地選擇實現。 – insitu 2012-03-28 15:49:00