编组错误 - 缺少xmlrootelement注释错误

时间:2016-10-15 13:11:05

标签: spring-boot jaxb marshalling xmlroot

当我从spring项目中调用其中一个WSDL操作时,我会遇到以下异常 - com.sun.istack.internal.SAXException2: unable to marshal type "com.pkg.wsdl.ABC" as an element because it is missing an @XmlRootElement annotation

我在pom.xml中使用以下命令从WSDL(已被许多客户端使用)生成java对象作为spring项目的一部分 -

        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.13.1</version>

查看类似的问题解决方案我改变了代码以使用JAXBElement但仍然得到相同的错误 -

    ABC vabc = new ABC();
    vabc.set(..)   // populate vabc object 

    ObjectFactory of = new ObjectFactory();
    JAXBElement<ABC> jabc = of.createABC(vabc);
    ABC oabc = jabc .getValue();

Marshaller Code -

Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.pkg.wsdl");

并调用后端Web服务 -

        ABCResp response = (ABCResp) getWebServiceTemplate()
        .marshalSendAndReceive("http://host:port/svcname",oabc);

1 个答案:

答案 0 :(得分:7)

我必须解决以下问题 -
1-缺少xmlRootElement注释错误
必须在marshalSendAndReceive中传递JAXBElement本身,如下所示 您可以从ObjectFactory中提取QName的确切详细信息。

2-请求错误中缺少soapAction
必须传递WebServiceMessageCallback函数,如下所示设置soapAction

3- classCastExcetion解组响应
必须添加JAXBIntrospector来修复此错误

ABCResp response = (ABCResp ) JAXBIntrospector.getValue(getWebServiceTemplate()
        .marshalSendAndReceive(
                "http://host:port/svcname",
                new JAXBElement<ABC>(new QName(uri, localpart),ABC.class,request),
                new WebServiceMessageCallback() {

                    public void doWithMessage(WebServiceMessage message) {
                        ((SoapMessage)message).setSoapAction("/test");
                    }
                }));