首页 > 解决方案 > Postman 在 SOAP 响应中返回 Null 值

问题描述

我是 SOAP 和 POSTMAN 的新手,我想知道我在以下方面做错了什么。我有一个使用 jax-ws 的示例 java webservice:

@WebService(endpointInterface = "Soap1.SOAPInterface")
public class SOAPService implements SOAPInterface
{
public String message(String name)
{
    return "Hello " + name;
}
}

我使用端点发布了这个网络服务:

public class Publisher 
{
public static void main(String[]args)
{
    Endpoint.publish("http://localhost:9006/Service", new SOAPService());
}
}

现在当我在客户端运行它时它工作正常

    public static void main(String[] args) throws Exception
    {
    URL url = new URL("http://localhost:9006/Service?wsdl");
    QName qname = new QName("http://Soap1/","SOAPServiceService");
    Service s = Service.create(url,qname);
    SOAPInterface i = s.getPort(SOAPInterface.class);
    System.out.println(i.message("Bob"));
    }

但是,当尝试使用 POSTMAN 分析 SOAP 请求/响应时。通过为请求输入以下 xml:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <MyMessage xmlns="http://Soap1/">
            <name>Bob</name>
        </MyMessage>
    </Body>
</Envelope>

我收到 Hello null 的回复

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:MyMessageResponse xmlns:ns2="http://Soap1/">
            <returnedMessage>Hello null</returnedMessage>
        </ns2:MyMessageResponse>
    </S:Body>
</S:Envelope>

我想知道为什么这是因为使用客户端,参数传递正常,但使用 POSTMAN 时,它们似乎没有传递。

邮递员截图: 邮差

标签: javasoappostmanjax-ws

解决方案


如果您在浏览器 URI http://localhost:9006/Service?wsdl中打开,您将看到 JAX-WS 为您的服务生成的 WSDL。它应该包含以下代码段:

<types>
<xsd:schema>
<xsd:import namespace="http://example.soap.kdv.org/" schemaLocation="http://localhost:9006/Service?xsd=1"/>
</xsd:schema>
</types>

它包含对定义在 Web 服务中使用的 XML 消息结构的 XML 模式的引用。如果你也打开 URI http://localhost:9006/Service?xsd=1(URI 可能不同,请检查),你会看到请求和响应消息的定义:

<xs:schema xmlns:tns="http://example.soap.kdv.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://example.soap.kdv.org/">
<xs:element name="message" type="tns:message"/>
<xs:element name="messageResponse" type="tns:messageResponse"/>
<xs:complexType name="message">
<xs:sequence>
<xs:element name="arg0" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="messageResponse">
<xs:sequence>
<xs:element name="return" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

它定义了以下请求消息的结构:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:exam="http://example.soap.kdv.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <exam:message>
         <!--Optional:-->
         <arg0>test</arg0>
      </exam:message>
   </soapenv:Body>
</soapenv:Envelope>

在邮递员中尝试此消息,它应该返回您想要的结果。

此外,我想推荐用于测试 Web 服务的 SOAP UI 工具。在此工具中创建新的 SOAP 项目时,它会导入 WSDL 并为您生成请求消息。


推荐阅读