无法将类型 <CLASSNAME> 编组为元素,因为它缺少 @XmlRootElement 注释

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

不是重复的因为我已经尝试了很多来自stackoverflow的解决方案,比如添加

@XmlRootElement(name="Generic")
并且还尝试将请求和响应对象包装在
javax.xml.bind.JAXBElement
中,但到目前为止对我没有任何作用。编组/解组一直失败,自从我调试和解决这个问题以来已经有 2 天了。

wsdl 文件很大,所以只在这里发布 WSDL 文件中的重要内容。

soap.wsdl

<types>
        <xs:schema xmlns:tns="http://www.oracle.com/UCM" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://www.oracle.com/UCM" version="1.0">
            <xs:element name="Field">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:string">
                            <xs:attribute name="name" type="xs:anySimpleType"/>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>
            <xs:element name="GenericRequest" type="tns:Generic"/>
            <xs:element name="GenericResponse" type="tns:Generic"/>
            <xs:complexType name="Generic">
                <xs:sequence>
                    <xs:element name="Service" type="tns:Service"/>
                </xs:sequence>
                <xs:attribute name="webKey" type="xs:anySimpleType"/>
            </xs:complexType>
            <xs:complexType name="Service">
                <xs:sequence>
                    <xs:element minOccurs="0" name="User" type="tns:Container"/>
                    <xs:element minOccurs="0" name="Document">
                        <xs:complexType>
                            <xs:complexContent>
                                <xs:extension base="tns:Container">
                                    <xs:sequence>
                                        <xs:element maxOccurs="unbounded" minOccurs="0" name="File" type="tns:File"/>
                                    </xs:sequence>
                                    <xs:anyAttribute namespace="##other" processContents="skip"/>
                                </xs:extension>
                            </xs:complexContent>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
                <xs:attribute name="IdcService" type="xs:anySimpleType"/>
            </xs:complexType>
            <xs:complexType name="Container">
                <xs:sequence>
                    <xs:element maxOccurs="unbounded" minOccurs="0" ref="tns:Field"/>
                    <xs:element maxOccurs="unbounded" minOccurs="0" name="ResultSet" type="tns:ResultSet"/>
                    <xs:element maxOccurs="unbounded" minOccurs="0" name="OptionList" type="tns:OptionList"/>
                </xs:sequence>
                <xs:anyAttribute namespace="##other" processContents="skip"/>
            </xs:complexType>
            <xs:complexType name="ResultSet">
                <xs:sequence>
                    <xs:element maxOccurs="unbounded" minOccurs="0" name="Row" type="tns:Row"/>
                </xs:sequence>
                <xs:attribute name="name" type="xs:anySimpleType"/>
            </xs:complexType>
            <xs:complexType name="Row">
                <xs:sequence>
                    <xs:element maxOccurs="unbounded" ref="tns:Field"/>
                </xs:sequence>
                <xs:anyAttribute namespace="##other" processContents="skip"/>
            </xs:complexType>
            <xs:complexType name="OptionList">
                <xs:sequence>
                    <xs:element maxOccurs="unbounded" name="Option" type="xs:string"/>
                </xs:sequence>
                <xs:attribute name="name" type="xs:anySimpleType"/>
            </xs:complexType>
            <xs:complexType name="File">
                <xs:sequence>
                    <xs:element xmlns:ns1="http://www.w3.org/2005/05/xmlmime" name="Contents" ns1:expectedContentTypes="application/octet-stream" type="xs:base64Binary"/>
                </xs:sequence>
                <xs:attribute name="name" type="xs:anySimpleType"/>
                <xs:attribute name="href" type="xs:anySimpleType"/>
            </xs:complexType>
        </xs:schema>
    </types>
    <message name="GenericSoapOperationInput">
        <part name="GenericRequest" element="ns0:GenericRequest"/>
    </message>
    <message name="GenericSoapOperationOutput">
        <part name="GenericResponse" element="ns0:GenericResponse"/>
    </message>
    <portType name="GenericSoapPortType">
        <operation name="GenericSoapOperation">
            <input xmlns:ns1="http://www.w3.org/2006/05/addressing/wsdl" message="tns:GenericSoapOperationInput" ns1:Action="urn:GenericSoap/GenericSoapOperation"/>
            <output xmlns:ns1="http://www.w3.org/2006/05/addressing/wsdl" message="tns:GenericSoapOperationOutput" ns1:Action="urn:GenericSoap/GenericSoapPortType/GenericSoapOperationResponse"/>
        </operation>
    </portType>
    <binding name="GenericSoapPortTypeSoapHttp" type="tns:GenericSoapPortType">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsp:PolicyReference xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" URI="#wss11_saml_or_username_token_with_message_protection_service_policy" wsdl:required="false"/>
        <wsp:PolicyReference xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" URI="#wsmtom_policy" wsdl:required="false"/>
        <operation name="GenericSoapOperation">
            <soap:operation soapAction="urn:GenericSoap/GenericSoapOperation"/>
            <input>
                <soap:body use="literal" parts="GenericRequest"/>
                <wsp:PolicyReference xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" URI="#GenericSoapPort_Input_Policy" wsdl:required="false"/>
            </input>
            <output>
                <soap:body use="literal" parts="GenericResponse"/>
                <wsp:PolicyReference xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" URI="#GenericSoapPort_Output_Policy" wsdl:required="false"/>
            </output>
        </operation>
    </binding>
    <service name="GenericSoapService">
        <port name="GenericSoapPort" binding="tns:GenericSoapPortTypeSoapHttp">
            <soap:address location="https://myenvtest.oraclecloud.com:443/idcws/GenericSoapPort"/>
            <wsa:EndpointReference xmlns:wsa="http://www.w3.org/2005/08/addressing">
                <wsa:Address xmlns:wsa="http://www.w3.org/2005/08/addressing">https://myenvtest.oraclecloud.com:443/idcws/GenericSoapPort</wsa:Address>
                
            </wsa:EndpointReference>
        </port>
    </service>

WSDL 的 Java 配置类:

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {


    @Bean
    public Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setContextPath("com.example.camel.client.stub");
        jaxb2Marshaller.setMarshallerProperties(new HashMap<>() {{
            put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true);
        }});
        return jaxb2Marshaller;
    }


    @Bean
    public Marshaller marshaller() {
        return jaxb2Marshaller().createMarshaller();
    }

    @Bean
    public Unmarshaller unmarshaller() {
       return jaxb2Marshaller().createUnmarshaller();
    }

    @Bean
    public SoapClient soapClient(Jaxb2Marshaller marshaller) {
        SoapClient client = new SoapClient();
        client.setDefaultUri("https://myenvtest.oraclecloud.com:443/idcws/GenericSoapPort");
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }

下面是将调用 SOAP 服务的 SoapClient

SoapClient.java

public javax.xml.bind.JAXBElement<Generic> getGenericResponse() throws JAXBException {
        Generic request = new ObjectFactory().createGeneric();
        Service service = new ObjectFactory().createService();
        Service.Document document = new ObjectFactory().createServiceDocument();
        Field field = new ObjectFactory().createField();
        Container container = new ObjectFactory().createContainer();
        container.getField().add(field);
        service.setUser(container);
        field.setName("dID");
        field.setValue("2088250");
        document.getField().add(field);
        service.setIdcService("GET_FILE");
        service.setDocument(document);
        request.setService(service);
        request.setWebKey("cs");



       /* jakarta.xml.bind.Marshaller marshaller = jaxb2Marshaller.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(new JAXBElement<Generic>(new QName("http://www.oracle.com/UCM","GenericRequest"), Generic.class, request),
                System.out);

        String t = marshallXml(request);*/

        javax.xml.bind.JAXBElement<Generic> res = ( javax.xml.bind.JAXBElement<Generic>)
        getWebServiceTemplate().marshalSendAndReceive("https://myenvtest.oraclecloud.com/idcws/GenericSoapPort", request, new SoapActionCallback("https://myenvtest.oraclecloud.com/idcws/Generic"));
        
return null;
    }

我不断收到的错误是:

com.sun.istack.SAXException2: unable to marshal type "com.example.camel.client.stub.Generic" as an element because it is missing an @XmlRootElement annotation
java oracle soap wsdl maven-jaxb2-plugin
© www.soinside.com 2019 - 2024. All rights reserved.