webService
一、简介
WebService即web服务,它是一种跨编程语言和跨操作系统平台的远程调用技术。
WebService是指一个应用程序向外界暴露了一个能通过Web调用的API接口,我们把调用这个WebService的应用程序称作客户端,把提供这个WebService的应用程序称作服务端。
二、接口验证(工具SoapUI)
1.soap版本验证
根据接口标准,查看SOAP Version确认版本为SOAP1.1还是SOAP1.2。
可以添加@BindingType(value= SOAPBinding.SOAP12HTTP_BINDING)使版本确定为SOAP1.2。
2.命名空间、soapAction验证
确认是否为wsdl要求的命名空间
@WebService(
serviceName = “xxxWebService”,//对外发布的服务名
targetNamespace = “http://www.xxx.com/webservice/”//指定 wsdl的命名空间
)
例:wsdl中约定了各接口的soapAction应该为“命名空间+接口名”
@WebMethod(action = “http://www.xxx.com/webservice/sayHello”)
3.elementFormDefault属性验证
Schema的这项属性约定了非全局的元素是否需要添加命名空间的前缀。wsdl中约定了这个属性的值为unqualified,即参数不需要添加前缀。
验证方式有两种:
一是访问接口的wsdl地址(若有xsd引用则访问对应xsd地址),查看schema的elementFormDefault属性,这个属性未设置时默认为unqualified,所以未找到该属性或者该属性值为unqualified时,符合wsdl定义;
二是通过soapui查看,如下图所示,箭头处不应带有“命名空间前缀+冒号”。
@WebParam(name = “param”) String name
4.数据结构验证
@WebResult(name = “result”)
三、示例代码
//对外发布的接口
public interface HelloService {
public String sayHello(String name);
}
//接口的实现类
@BindingType(value= SOAPBinding.SOAP12HTTP_BINDING)
@WebService(
serviceName = "xxxWebService",//对外发布的服务名
targetNamespace = "http://www.xxx.com/webservice/"//指定 wsdl的命名空间
)
public class HelloServiceImpl implements HelloService {
@WebMethod(action = "http://www.xxx.com/webservice/sayHello")
@WebResult(name = "result")
public String sayHello(@WebParam(name = "param") String name) {
return "hello,"+name;
}
}
//发布服务
public class Server {
public static void main(String[] args) {
String address = "http://127.0.0.1:8282/hello";
Endpoint.publish(address,new HelloServiceImpl());
System.out.println("服务已发布");
}
}
工具SoapUI:
设置的地址+?wsdl
http://127.0.0.1:8282/hello?wsdl