跳到主要内容

SOAP Web Service

SOAP 使用 XML 消息调用 Web Service,通过 WSDL 描述服务接口,适合对接已有 Web Service 系统。

  • 支持 SOAP 1.1、1.2,默认使用 1.1。
  • 支持请求响应、单向操作,以及 inline、MIME、MTOM 附件。
  • 可根据 Java 接口生成 WSDL,也可加载已有 WSDL;不支持 RPC 消息流。

引入依赖​

<dependency>
<groupId>net.hasor</groupId>
<artifactId>meteor-protocol-soap</artifactId>
<version>5.0.0-SNAPSHOT</version>
</dependency>

启用协议​

提供者监听端口并提供服务,消费者通过出站协议调用服务。

提供者
configuration.connector("http")
.bind("127.0.0.1", 8080)
.protocol("soap").contextPath("/soap");
消费者
configuration.connector("http")
.protocol("soap")
.contextPath("/soap");

发起 RPC 调用​

在已启动的 MetContext 中发布 EchoService,客户端调用 echo,输出 hello。

服务定义
import net.hasor.meteor.annotation.MetParam;

public interface EchoService {
String echo(@MetParam(id = 1) String text);
}

public class EchoServiceImpl implements EchoService {
public String echo(String text) { return text; }
}
提供者
context.publisher().rsfService(EchoService.class, new EchoServiceImpl())
.name("meteor.example.EchoService")
.protocol("soap")
.aliasName("soap", "echo")
.register();

提供者自动生成 WSDL:http://127.0.0.1:8080/soap/default/1.0.0/echo?wsdl。

消费者
import net.hasor.meteor.address.InterAddress;

// 订阅服务接口
var binding = context.publisher().rsfService(EchoService.class)
.name("meteor.example.EchoService")
.protocol("soap")
.aliasName("soap", "echo")
.register();

// 通过直连地址获取代理
EchoService client = context.getRsfClient(new InterAddress("soap", "127.0.0.1", 8080))
.getRemote(binding);

// 调用服务
System.out.println(client.echo("hello"));

协议配置​

已有 WSDL 使用 SoapWsdlResource.load("classpath:echo.wsdl") 加载,通过 bindResource("soap", resource) 绑定。

通过 protocol.option("名称", 值) 设置协议选项;配置文件使用 meteor.connectors.soap.options.<名称>。

  • soapVersion:默认 1.1。
  • soapUse:默认 literal;soapStyle、soapParameterStyle 设置消息形式。
  • attachmentMode:默认 inline;attachmentThreshold:默认 1024 字节。
  • publicBaseUrl:设置向客户端公开的服务地址;soapActionRequired:控制 SOAP Action 校验。

XML 名称​

Java 方法、参数和类型名称与 SOAP 名称不同时,用公共注解指定:

import net.hasor.meteor.annotation.MetEnumValue;
import net.hasor.meteor.annotation.MetField;
import net.hasor.meteor.annotation.MetMethod;
import net.hasor.meteor.annotation.MetParam;
import net.hasor.meteor.annotation.MetReturn;
import net.hasor.meteor.annotation.MetType;

public interface OrderService {
@MetMethod(name = "getOrder")
@MetReturn(name = "order")
Order find(@MetParam(name = "orderId") String id);
}

@MetType(name = "Order")
public class Order {
@MetField(name = "orderId")
public String id;
public OrderState state;
}

public enum OrderState {
@MetEnumValue(name = "pending")
Pending,
@MetEnumValue(name = "paid")
Paid
}

SOAP 操作为 getOrder,输入成员为 orderId,返回成员为 order;返回类型为 Order,属性 id 写为 orderId,枚举 Paid 写为文本 paid。命名空间、XML 属性和集合包装继续使用 Jackson XML 注解;已有 WSDL 的名称和结构仍需匹配。

注意事项​

  • 客户端和提供者须使用一致的命名空间、Action、元素名称和 Fault 定义。
  • 附件传输不等于流式 RPC。
  • XML 属性、文本和集合包装使用 Jackson XML 注解;精确的 XML 结构可由 WSDL/XSD 描述。