为生成的 class 添加 xml 根元素注释的方法?

Way to add xml root element annotation for generated class?

我已经从 WSDL 文件生成了 JAXB classes,我想做的是将 XML 转换为 Java 对象。这是生成的 JAXB class 示例:

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;

...
}

我需要转换为 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>

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.");
    }
}

问题是生成的 JAXB class GetProductListResponse 不包含 @XmlRootElement 注释,因此此转换失败并出现著名的错误消息 javax.xml.bind.UnmarshalException: unexpected element ... Expected elements are ...

当我手动给GetProductListResponseclass添加@XmlRootElement注解,并设置为:

@XmlRootElement(name="GetProductsListResult")
public class GetProductListResponse { ...}

转换成功。

问题: 有什么方法可以从 class 外部为生成的 class (GetProductListResponse ) 设置 @XmlRootElement?

我想避免自定义生成的 class 并且我不想更改 WSDL 定义。我还阅读了有关设置运行时注释的信息,但我想避免使用任何 Java 字节码操纵器(如 Javassist)。

这在 Jackson json/xml parser library. Jackson fully supports JAXB annotations and its mixin feature 中是可能的,允许您从源代码外部向 Class 添加注释。

    JAXBContext jaxbContext = JAXBContext.newInstance(GetProductListResponse.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    JAXBElement<GetProductListResponse> root = jaxbUnmarshaller.unmarshal(new StreamSource(
            file), GetProductListResponse.class);
    GetProductListResponse productListResponse = root.getValue();

对于缺少@XmlRootElement,这篇文章对我帮助很大。看一看:

http://www.source4code.info/2013/07/jaxb-marshal-unmarshal-with-missing.html

你可以在这里找到你的错误,看看你能做什么!希望能帮助到你 !