WSDLファイルからJAXBクラスを生成しましたが、XMLをJavaオブジェクトに変換しようとしています。ここで生成されたJAXBクラスの例:XMLの生成されたクラスにxmlルート要素注釈を追加する方法は?
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
オブジェクトに変換する必要がありproducts.xml
ファイル内に保存されており、それは次のようになります。
convertXmlProductsTest
方法がある
<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>
内部コンバージョンコールが設定されている場所:
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クラスGetProductListResponse
には@XmlRootElement
注釈が含まれていないため、この変換は有名なエラーメッセージjavax.xml.bind.UnmarshalException: unexpected element ... Expected elements are ...
で失敗します。変換が成功した
@XmlRootElement(name="GetProductsListResult")
public class GetProductListResponse { ...}
:私は手動でGetProductListResponse
クラスに@XmlRootElement
注釈を追加し、としてそれを設定
。
QUESTION: は、そのクラスの外から生成されたクラス(GetProductListResponse
)について@XmlRootElement
を設定する方法はありますか?
生成されたクラスのカスタマイズを避けたいと思い、WSDL定義を変更したくありません。また、ランタイムアノテーションの設定についても読んでいますが、JavassistなどのJavaバイトコードマニピュレータを使用する必要はありません。
お返事ありがとう@sharonbn!私はあなたが示唆したようにJacksonのライブラリを試しましたが、生成されたクラスの中にはセッターやアーギュメントのないコンストラクタ(つまりArrayOfProductListDetail)が生成されていないため、XMLを解析するのに問題があります。私はまた、mavenでjaxb annotateプラグインを使用し、バインディングjxbファイルを追加しようとしましたが、xsdファイルがひどく書かれているため、maven-jaxb2コンパイラは報告された衝突エラーを含むxsdファイル内の数百箇所。他に提案はありますか?前もって感謝します。 – hideburn
申し訳ありませんが、他の提案はありません。 –