[英]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.