[英]JAX-RS: Retrieving an attribute from a post() Response
I need to retrieve an attribute from a Response
object which is returned from a post()
invocation: in particular, I'm using Neo4J and after posting a node I would like to retrieve its Id, which is an attribute in the returned XML code. 我需要从通过
post()
调用返回的Response
对象中检索一个属性:特别是,我正在使用Neo4J,并且在发布节点后,我想检索其ID,这是返回的XML代码中的一个属性。 。 My current post looks like this: 我当前的帖子如下所示:
Response res = target.path("resource/node").request(MediaType.APPLICATION_XML)
.post(Entity.entity(node, MediaType.APPLICATION_XML));
Then I perform a check on the returned HTTP status and I would also need the node Id, which is returned in: 然后,我对返回的HTTP状态进行检查,我还需要在以下位置返回的节点ID:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<node xmlns="http://www.namespace.org/Neo4J" id="140">
... node properties ...
</node>
I tried to cast res.getEntity()
to a Document
but it causes the following: 我试图将
res.getEntity()
转换为Document
但会导致以下情况:
java.lang.ClassCastException: org.glassfish.jersey.client.internal.HttpUrlConnector$2 cannot be cast to org.w3c.dom.Document
Thanks in advance. 提前致谢。
You can use JAXB to map POST body XML data to java object: 您可以使用JAXB将POST正文XML数据映射到java对象:
Payload entity = res.getEntity(Payload.class);
String id = payload.id;
where Payload you can define to reflect your XML structure: 您可以在其中定义有效载荷以反映您的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.