gpt4 book ai didi

java - 带有 JAXB 的 WebService - 参数包含 null

转载 作者:太空宇宙 更新时间:2023-11-04 14:11:15 24 4
gpt4 key购买 nike

我正在尝试编写简单的网络服务来测试某些东西。我有以下简单请求 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();
}
}
}

运行时,我收到以下关于空指针参数的奇怪错误。怎么了 ?我尝试过调试,但它直接进入一些内部的 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 中的公共(public)类型。所以解决方法是使用:

<?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>

关于java - 带有 JAXB 的 WebService - 参数包含 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28302005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com