繁体   English   中英

JAX-RS:从post()响应中检索属性

[英]JAX-RS: Retrieving an attribute from a post() Response

我需要从通过post()调用返回的Response对象中检索一个属性:特别是,我正在使用Neo4J,并且在发布节点后,我想检索其ID,这是返回的XML代码中的一个属性。 。 我当前的帖子如下所示:

Response res = target.path("resource/node").request(MediaType.APPLICATION_XML)
              .post(Entity.entity(node, MediaType.APPLICATION_XML));

然后,我对返回的HTTP状态进行检查,我还需要在以下位置返回的节点ID:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<node xmlns="http://www.namespace.org/Neo4J" id="140">
 ... node properties ...
</node>

我试图将res.getEntity()转换为Document但会导致以下情况:

java.lang.ClassCastException: org.glassfish.jersey.client.internal.HttpUrlConnector$2 cannot be cast to org.w3c.dom.Document

提前致谢。

您可以使用JAXB将POST正文XML数据映射到java对象:

Payload entity = res.getEntity(Payload.class);
String id = payload.id;

您可以在其中定义有效载荷以反映您的XML结构:

import javax.xml.bind.annotation.*;


@XmlRootElement(name="node")
@XmlAccessorType(XmlAccessType.FIELD)
public class Payload {
    @XmlAttribute(name = "id")
    String id; // for example
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM