带有JAXB的WebService - 参数包含null

时间:2015-02-03 15:00:50

标签: java web-services xsd jaxb

我正在尝试编写简单的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)

2 个答案:

答案 0 :(得分:0)

编辑:我发现当我将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>

答案 1 :(得分:0)

对于今天仍然对这个错误感到困惑的任何人,我都有同样的问题,发现这是由实现 Clonable 接口的端点的响应类型中的类引起的。删除即可解决问题。请确保在删除它时不会破坏其他功能。