Java、WSDL 生成的类无法转换为 javax.xml.bind.JAXBElement

问题描述 投票:0回答:1

调用 Web 服务后,我返回 javax.xml.bind.JAXBElementException。

JAXBElement<ShowPendingFiles> jAXBElement = new ObjectFactory().createShowPendingFiles(showPendingFilesRequest);

CustomEnvelopeDetails customEnvelopeDetails = new CustomEnvelopeDetails(
    CcrConstants.ACTION_PENDING_FILES,
    new SecurityHeader(isSecurityEnabled, username, password)
        );

final Object marshalled = (ShowPendingFilesResponse) createServiceTemplate(jaxb2Marshaller)
//return (ShowPendingFilesResponse) createServiceTemplate(jaxb2Marshaller)
    .marshalSendAndReceive(soapServer, jAXBElement,
          new SoapRequestHeaderModifier(customEnvelopeDetails));
return (ShowPendingFilesResponse) marshalled;

Web 服务响应 200 OK,XML 格式的响应为

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns2:showPendingFilesResponse
            xmlns:ns2="http://iris.somewhere.com/web_services/external/downloadFile"
            xmlns:ns3="http://iris.somewhere.gr/web_services/external/uploadFile"/>
    </soapenv:Body>
</soapenv:Envelope>

这是正确的,因为还没有“可供下载的文件”。

但是 marshalSendAndReceive 无法编组响应。

我遇到以下异常:

javax.xml.bind.JAXBElement 无法转换为 下载.iris.models.ShowPendingFilesResponse

自动生成的(从 WSDL)JAX 类就是这个

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "showPendingFilesResponse", propOrder = {
    "showPendingFilesResult"
})
public class ShowPendingFilesResponse {

    @XmlElement(nillable = true)
    protected List<TransferableFile> showPendingFilesResult;

   
    public List<TransferableFile> getShowPendingFilesResult() {
        if (showPendingFilesResult == null) {
            showPendingFilesResult = new ArrayList<TransferableFile>();
        }
        return this.showPendingFilesResult;
    }

} 
java spring jaxb wsdl
1个回答
0
投票

从您发送的请求中可以看出

JAXBElement<ShowPendingFiles> jAXBElement = new ObjectFactory().createShowPendingFiles(showPendingFilesRequest);

请求被包装在

JAXBElement
中,您需要对其进行反向解包。因为你得到的结果是(从你得到的异常来看)一个
JAXBElement<ShowPendingFilesResponse>
而不是直接
ShowPendingFilesResponse

相应地修改您的代码。

final JAXBElement<ShowPendingFilesResponse> marshalled = (JAXBElement<ShowPendingFilesResponse>) createServiceTemplate(jaxb2Marshaller)
    .marshalSendAndReceive(soapServer, jAXBElement, new SoapRequestHeaderModifier(customEnvelopeDetails));
return marshalled.getValue();
© www.soinside.com 2019 - 2024. All rights reserved.