java - 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 时,它们似乎没有传递。
解决方案
如果您在浏览器 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 并为您生成请求消息。
推荐阅读
- java - 使用扫描仪获取“二元运算符'/'的错误操作数类型”错误:(
- sql - 基于列值的 PSQL 约束
- php - var 需要是 url 链接
- algorithm - 执行 Dijkstra 时是否可以确定跳数?
- google-cloud-functions - 有没有办法在多个谷歌存储桶中跟踪文件信息(大小、名称、存储桶位置、创建/更新时间戳)
- python - 在pyplot中显示二维坐标图
- python - 梯度下降出现在机器学习库的什么地方(例如 scikitlearn)
- php - 如何清除opencart网站的缓存文件夹
- python - 从 Python 列表中删除重复项的更好解决方案
- django - 为什么 Django 发送电子邮件功能会发送两次电子邮件?