[英]Xml to Java Class using JAXBContext
我創建了幾個 REST Web 服務,它們將從我的數據庫中返回記錄列表。
我以 XML 格式返回所有查詢結果,並在我的 Web 服務方法中將其轉換為 Java 以將其以 JSON 格式發送到我的客戶端應用程序(我知道,這不是最好的情況)。
我試圖減少我的方法中的代碼,我開始使用的代碼的一部分是 XML 到 Java 的轉換。
現在我正在使用以下
String xml = xml.replaceFirst("ROWSET xmlns:xsi = " + "\"http://www.w3.org/2001/XMLSchema-instance\"", "invTrxXmlList");
String string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
xml = string.concat(xml);
xml = xml.replaceAll("ROWSET", "invTrxXmlList");
xml = xml.replaceAll("ROW", "invTrxXmlList");
xml = xml.replaceAll(" xsi:nil = \"true\"", "");
InputStream instr = new ByteArrayInputStream(xml.getBytes());
JAXBContext jaxbContext = JAXBContext.newInstance(InvTrxXmlList.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
InvTrxXmlList invTrxXml = (InvTrxXmlList) jaxbUnmarshaller.unmarshal(instr);
inventoryTrx = new InventoryTrx(invTrxXml);
我想要的是創建一個單獨的方法,該方法可以被該作業的所有方法調用,如果可能的話,可以使用多個不同的自定義對象,例如 InvTrxXmlList。
我在另一項任務中使用了 Reflection,我正在考慮使用它來解決這個問題。
你能幫助我嗎?
還有另一種方法嗎?
謝謝
創建一個Java類1. @ Path(“ / URL”)2.類名3. @GET或@POST 4. @Path(“ / URL”)5. @Produces(“ application / xml”)JAXBElement getProduct()然后,您可以使用DOJO來獲取或設置數據
以下代碼顯示了如何使用生成的類在JAX-RS資源方法中以XML形式返回JAXB元素:
@Path("/product")
public class ProductService {
@GET
@Path("/get")
@Produces("application/xml")
public JAXBElement<Product> getProduct() {
Product prod = new Product();
prod.setId(1);
prod.setName("Mattress");
prod.setDescription("Queen size mattress");
prod.setPrice(500);
return new ObjectFactory().createProduct(prod);
}
}
對於@POST和@PUT資源方法,可以將Product對象直接用作參數。 JAX-RS將XML數據從請求映射到Product對象。
@Path("/product")
public class ProductService {
@GET
// ...
@POST
@Path("/create")
@Consumes("application/xml")
public Response createProduct(Product prod) {
// Process or store the product and return a response
// ...
}
}
謝謝西爾弗雷德,
我能夠將其與以下代碼一起使用
public Object xmlToJava(Object pCurrentObject, String pXml, String pRowset, String pRow) {
InputStream instr = null;
JAXBContext jaxbContext = null;
Unmarshaller jaxbUnmarshaller = null;
String xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
String xmlReturn = null;
Class objectClass = pCurrentObject.getClass();
try {
xmlReturn =
pXml.replaceFirst("ROWSET xmlns:xsi = " +
"\"http://www.w3.org/2001/XMLSchema-instance\"",
pRowset); //"invTrxLines");
xmlReturn = xmlHeader.concat(xmlReturn);
xmlReturn = xmlReturn.replaceAll("ROWSET", pRowset); //"invTrxLines");
if (null != pRow && !pRow.isEmpty()) {
xmlReturn = xmlReturn.replaceAll("ROW", pRow); //invTrxLine);
} else { //Remove Row tags in case of single object
xmlReturn = xmlReturn.replaceAll("<ROW>", "");
xmlReturn = xmlReturn.replaceAll("</ROW>", "");
}
xmlReturn = xmlReturn.replaceAll(" xsi:nil = \"true\"", "");
instr = new ByteArrayInputStream(xmlReturn.getBytes());
jaxbContext = JAXBContext.newInstance(objectClass); //InvTrxHead.class);
jaxbUnmarshaller = jaxbContext.createUnmarshaller();
pCurrentObject = jaxbUnmarshaller.unmarshal(instr);
} catch (Exception e) {
// TODO: Add catch code
e.printStackTrace();
}
return pCurrentObject;
}
與以下電話
invTrxHead = (InvTrxHead) WSUtils.xmlToJava(invTrxHead, invTrxHeadXml, "invTrxHead", null);
現在,我可以從數據庫和對象實例中提取XML,而不必重復任何代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.