什么是webservice
为了支持跨网络的机器间相互操作交互而设计。Web Service服务通常被定义为一组模块化的API,它们可以通过网络进行调用,来执行远程系统的请求服务
WebService三要素
SOAP (Simple Object Access Protocol):简易对象访问协议,soap用来描述传递信息的格式。
WSDL (WebServices Description Language):Web服务描述语言,用来描述WebService、以及如何访问WebService
UDDI (Universal Description Discovery and Integration):通用描述、发现及整合,用来管理、分发、
springboot中如何集成webservice
服务端
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.18.Final</version>
</dependency>
接口类
@WebService
public interface WebServiceSay {
@WebMethod
public String say(@WebParam(name = "name") String name,@WebParam(name = "value")String value);
}
@WebService(serviceName = "WebServiceSay",//对外发布的服务名
targetNamespace = "http://service.webservice.example.com/",//指定你想要的名称空间,通常使用使用包名反转
endpointInterface = "com.example.webservice.service.WebServiceSay")
@Service
public class WebServiceSayImpl implements WebServiceSay {
@Override
public String say(String name, String value) {
return name+" say: "+value;
}
}
配置类
@Configuration
public class WebServiceConfig {
@Autowired
private WebServiceHello webServiceHello;
@Autowired
private WebServiceSay webServiceSay;
/**
* wsdl访问地址为:http://127.0.0.1:8080/webservice/XXX?wsdl
* @return
*/
@Bean(name = "cxfServlet")
public ServletRegistrationBean cxfServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
//如果有其余的服务,继续加
@Bean(name = "webServiceSayEndPoint")
public Endpoint webServiceSayEndPoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), webServiceSay);
endpoint.publish("/webServiceSay");
return endpoint;
}
}
启动
访问 http://127.0.0.1:8080/webservice/WebServiceSay?wsdl
SoapUI测试webservice
客户端调用
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://127.0.0.1:8080/webservice/webServiceSay?wsdl");
try {
// invoke("方法名",参数1,参数2,参数3....);
Object[] res = client.invoke("say", "张三","this is val");
String s = JSON.toJSONString(res[0]);
System.out.println("返回数据:" + s);
} catch (java.lang.Exception e) {
e.printStackTrace();
}