带有JAXB的WebService - 参数包含null

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带有JAXB的WebService - 参数包含null相关的知识,希望对你有一定的参考价值。

我正在尝试编写简单的Web服务来测试一些东西。我有以下简单的请求XSD:

<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://processonlinejob.ws/processOnlineJobRequest" targetNamespace="http://processonlinejob.ws/processOnlineJobRequest" elementFormDefault="qualified">
    <element name="processOnlineJobRequest">
        <complexType>
            <sequence>
                <element name="template">
                    <complexType>
                        <sequence>
                            <element name="name" type="string"/>
                            <element name="version" type="string"/>
                        </sequence>
                    </complexType>
                </element>
                <element name="data" type="tns:processOnlineJobData"/>
            </sequence>
        </complexType>
    </element>
    <complexType name="processOnlineJobData">
        <sequence maxOccurs="unbounded">
            <any processContents="skip"/>
        </sequence>
    </complexType>
</schema>

以下简单的响应XSD:

<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://processonlinejob.ws/processOnlineJobResponse" targetNamespace="http://processonlinejob.ws/processOnlineJobResponse" elementFormDefault="qualified">
    <element name="processOnlineJobResponse" type="tns:processOnlineJobResponse"/>
    <complexType name="processOnlineJobResponse">
        <sequence>
            <element name="resultCode" type="short" minOccurs="1" maxOccurs="1"/>
            <element name="resultMessage" type="string" maxOccurs="1"/>
            <element name="pdfBase64" type="base64Binary" maxOccurs="1"/>
        </sequence>
    </complexType>
</schema>

我通过Eclipse(Spring工具套件3.6.3 SR1)自动生成了JAXB类,并编写了以下简单代码:

@WebService
@SOAPBinding(style = Style.RPC)
public interface ProcessOnlineJob {
    @WebMethod
    ProcessOnlineJobResponse processOnlineJob(ProcessOnlineJobRequest request);
}

@WebService(endpointInterface = "poc.ondemmand.ProcessOnlineJob")
public class ProcessOnlineJobImpl implements ProcessOnlineJob {
    @Override
    public ProcessOnlineJobResponse processOnlineJob(
            ProcessOnlineJobRequest request) {
        ProcessOnlineJobResponse response = new ProcessOnlineJobResponse();
        response.setResultCode((short) 1);
        response.setResultMessage("OK");
        byte[] data = { 1 };
        response.setPdfBase64(data);
        return response;
    }
}

public class Program {
    public static void main(String[] args) {
        try {
            Endpoint.publish("http://localhost:9999/ws/processonlinejob",
                    new ProcessOnlineJobImpl());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

运行时,我获得有关空指针参数的以下奇怪错误。怎么了 ?我试图调试,但它直接进入一些internall sun jaxb库类:-(。

java.lang.IllegalArgumentException: argument contains null
    at com.sun.xml.internal.txw2.Document.writeValue(Document.java:130)
    at com.sun.xml.internal.txw2.Document.writeValue(Document.java:134)
    at com.sun.xml.internal.txw2.StartTag.addAttribute(StartTag.java:120)
    at com.sun.xml.internal.txw2.ContainerElement._attribute(ContainerElement.java:303)
    at com.sun.xml.internal.txw2.ContainerElement.addAttribute(ContainerElement.java:160)
    at com.sun.xml.internal.txw2.ContainerElement.invoke(ContainerElement.java:134)
    at com.sun.proxy.$Proxy30.type(Unknown Source)
    at com.sun.xml.internal.ws.wsdl.writer.WSDLGenerator.generateSOAPMessages(WSDLGenerator.java:573)
    at com.sun.xml.internal.ws.wsdl.writer.WSDLGenerator.generateMessages(WSDLGenerator.java:545)
    at com.sun.xml.internal.ws.wsdl.writer.WSDLGenerator.generateDocument(WSDLGenerator.java:436)
    at com.sun.xml.internal.ws.wsdl.writer.WSDLGenerator.doGeneration(WSDLGenerator.java:321)
    at com.sun.xml.internal.ws.db.DatabindingImpl.generateWSDL(DatabindingImpl.java:242)
    at com.sun.xml.internal.ws.server.EndpointFactory.generateWSDL(EndpointFactory.java:622)
    at com.sun.xml.internal.ws.server.EndpointFactory.create(EndpointFactory.java:277)
    at com.sun.xml.internal.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:144)
    at com.sun.xml.internal.ws.api.server.WSEndpoint.create(WSEndpoint.java:563)
    at com.sun.xml.internal.ws.api.server.WSEndpoint.create(WSEndpoint.java:545)
    at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.createEndpoint(EndpointImpl.java:308)
    at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(EndpointImpl.java:231)
    at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(ProviderImpl.java:126)
    at javax.xml.ws.Endpoint.publish(Endpoint.java:240)
    at poc.ondemmand.Program.main(Program.java:22)
答案

编辑:我发现当我将XSD更改为以下内容时,一切正常。在我看来,JAXB在XSD中破坏了公共类型。所以解决方法是使用:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://processonlinejob.ws/processOnlineJobRequest" targetNamespace="http://processonlinejob.ws/processOnlineJobRequest" elementFormDefault="qualified">
    <element name="processOnlineJobRequest" type="tns:processOnlineJobRequest"/>
    <complexType name="processOnlineJobData">
        <sequence maxOccurs="unbounded">
            <any processContents="skip"/>
        </sequence>
    </complexType>
    <complexType name="templateType">
        <sequence>
            <element name="name" type="string"/>
            <element name="version" type="string"/>
        </sequence>
    </complexType>
    <complexType name="processOnlineJobRequest">
        <sequence>
            <element name="template" type="tns:templateType"/>
            <element name="data" type="tns:processOnlineJobData"/>
        </sequence>
    </complexType>
</schema>
另一答案

对于今天仍然遇到此错误的人,我遇到了同样的问题,发现它是由实现Clonable接口的端点的响应类型中的类引起的。删除它可以解决问题。请确保在删除它时不会破坏其他功能。

以上是关于带有JAXB的WebService - 参数包含null的主要内容,如果未能解决你的问题,请参考以下文章

CXF+JAXB处理复杂数据

当节点包含值时,使用 JAXB 将 XML 转换为对象

Java - 带有可选字段的 JAXB XML 解组

带有 JAXB 类的 GXT

Jaxb的有点与用法(bean转xml的插件,简化webservice接口的开发工作量)

一个CXF集成SPRING的WEBSERVICE完整实例