【问题标题】:Jersey - Pojo communication works, getEtntity doesntJersey - Pojo 通信有效,getEntity 无效
【发布时间】:2012-06-27 08:50:21
【问题描述】:

我在休息服务中使用this 第 4.3 章与 pojos 进行通信。目前我可以发布并获取pojo。它工作正常。但是,当我尝试获取响应的实体时,我得到了这个异常:

 27.06.2012 10:44:44 com.sun.jersey.api.client.ClientResponse getEntity
 SCHWERWIEGEND: A message body reader for Java class javax.xml.bind.JAXBElement, and  Java type class javax.xml.bind.JAXBElement, and MIME media type application/xml was not found
 27.06.2012 10:44:44 com.sun.jersey.api.client.ClientResponse getEntity
 SCHWERWIEGEND: The registered message body readers compatible with the MIME media type are:
 application/xml ->

在服务器端,我像这样包装实体:

  res = Response.created(UriBuilder.fromUri(uriInfo.getAbsolutePath() + "/" + object.getObjectId()).build()).entity(new JAXBElement<ObjectPOJO>(new QName("objectpojo"), ObjectPOJO.class, object)).build();

我也试过了,只是在没有 JAXBElement 的情况下包装 pojo。

在客户端我尝试了不同的方法:

 GenericType<JAXBElement<ObjectPOJO>> objectType = new GenericType<JAXBElement<ObjectPOJO>>() {};
 objectType = (GenericType<JAXBElement<ObjectPOJO>>) res.getEntity(JAXBElement.class).getValue();

 object = res.getEntity(ObjectPOJO.class);

等等。有谁知道什么是正确的方法?就像我说的,get 和 post 通信工作正常。

【问题讨论】:

    标签: http rest jersey pojo


    【解决方案1】:

    应该是:

    ObjectPOJO object = res.getEntity(new GenericType<JAXBElement<ObjectPOJO>>() {}).getValue();
    

    【讨论】:

    • 谢谢伙计,你过去帮了我很多。一个错误:应该是 getValue()
    猜你喜欢
    • 2016-10-02
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    相关资源
    最近更新 更多