hid*_*urn 4 java xml wsdl jaxb
我已经从 WSDL 文件生成了 JAXB 类,我想要做的是将 XML 转换为 Java 对象。这是生成的 JAXB 类示例:
XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "GetProductListResponse", propOrder = {
"productList",
"productListDate",
"failed",
"failedDescription"
})
public class GetProductListResponse {
@XmlElementRef(name = "ProductList", namespace = "http://productService.productsdata", type = JAXBElement.class, required = false)
protected JAXBElement<ArrayOfProductListDetail> productList;
@XmlElementRef(name = "ProductListDate", namespace = "http://productService.productsdata", type = JAXBElement.class, required = false)
protected JAXBElement<String> productListDate;
@XmlElement(name = "Failed")
protected boolean failed;
@XmlElement(name = "FailedDescription", required = true, nillable = true)
protected String failedDescription;
...
}
Run Code Online (Sandbox Code Playgroud)
我需要转换为GetProductListResponse对象的 XML 示例存储在products.xml文件中,它看起来像这样:
<GetProductListResult xmlns="http://productService.productsdata">
<ProductList>
<ProductListDetail>
<ProductName>SomeProductName</ProductName>
<ProductCost>9,45</ProductCost>
</ProductListDetail>
<ProductListDate>09.09.2015</ProductListDate>
<Failed>false</Failed>
<FailedDescription/>
</ProductList>
</GetProductListResult>
Run Code Online (Sandbox Code Playgroud)
convertXmlProductsTest方法内部是设置转换调用的地方 -jaxb unmarshaller为此目的使用:
public class ProductHandler {
public static GetProductListResponse convertXmlProductsTest(){
try {
JAXBContext jaxbContext = JAXBContext.newInstance(GetProductListResponse.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
GetProductListResponse retval = (GetProductListResponse) jaxbUnmarshaller.unmarshal(new File("products.xml"));
return retval;
} catch (JAXBException ex) {
Logger.getLogger(ProductMockWs.class.getName()).log(Level.SEVERE, null, ex);
}
throw new UnsupportedOperationException("XML to Java object conversion failed.");
}
}
Run Code Online (Sandbox Code Playgroud)
问题是生成的 JAXB 类GetProductListResponse不包含@XmlRootElement注释,因此此转换失败并显示著名的错误消息javax.xml.bind.UnmarshalException: unexpected element ... Expected elements are ...。
当我手动@XmlRootElement向GetProductListResponse类添加注释时,并将其设置为:
@XmlRootElement(name="GetProductsListResult")
public class GetProductListResponse { ...}
Run Code Online (Sandbox Code Playgroud)
转换成功。
问题:
有什么方法可以从该类之外设置@XmlRootElement生成的类 ( GetProductListResponse)?
我想避免自定义生成的类,并且不想更改 WSDL 定义。我还阅读了有关设置运行时注释的内容,但我想避免使用任何 Java 字节码操纵器(如 Javassist)的需要。
JAXBContext jaxbContext = JAXBContext.newInstance(GetProductListResponse.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<GetProductListResponse> root = jaxbUnmarshaller.unmarshal(new StreamSource(
file), GetProductListResponse.class);
GetProductListResponse productListResponse = root.getValue();
Run Code Online (Sandbox Code Playgroud)
对于缺少@XmlRootElement,这篇文章对我帮助很大。看一看 :
http://www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html
你可以在这里找到你的错误,看看你能做什么!希望能帮助到你 !
| 归档时间: |
|
| 查看次数: |
11615 次 |
| 最近记录: |