我遇到了这个异常。
我的对象看起来像这样:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ObjectDTO implements Serializable {
/**
*
*/
private static final long serialVersionUID = -8545841080597549468L;
@XmlElement(name="objectId")
private String objectId;
@XmlElement(name="owner")
private String owner;
@XmlElement(name="objectName")
private String objectName;
Constructor, getter/setter, toString, Hash...
现在我用以下方式调用我的服务方法:
ClientResponse postRes = service.path("rest").path("object").accept(MediaType.APPLICATION_XML).post(ClientResponse.class, object);
当我这样做的时候
System.out.println(postRes.getEntity(ObjectDTO.class));
效果很好。但是当我尝试对我的对象执行 get 操作时,例如:
String string = postRes.getEntity(ObjectDTO.class).getObjectId();
我得到了异常(exception)。
javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException: Premature end of life.]
怎么了?
最佳答案
看起来您正在尝试读取实体两次(第二次实体流已被消耗)。不要调用 postRes.getEntity() 两次,而只需调用一次,将结果存储在变量中,然后对该变量进行操作。
关于java - Jersey UnmarshalException JAXB,带有多个 getEntity() 的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11194027/