WebService
**
WebService简介
**
Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。
简单的说,WebService就是一种跨编程语言和跨操作系统平台的远程调用技术。所谓跨编程语言和跨操作平台,就是说服务端程序采用java编写,客户端程序则可以采用其他编程语言编写,反之亦然。跨操作系统平台则是指服务端程序和客户端程序可以在不同的操作系统上运行。 远程调用,就是一台计算机的应用可以调用其他计算机上的应用。例如:支付宝,支付宝并没有银行卡等数据,它只是去调用银行提供的接口来获得数据。还有天气预报等,也是气象局把自己的系统服务以webservice服务的形式暴露出来,让第三方网站和程序可以调用这些服务功能。
引入依赖
<!-- cxf Web Service -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
这里用到的是cxf的3.1.6版本,经过测试如果使用3.1.6Spring boot只能支持到2.0.3,使用2.0.4都会启动报错。
发布服务用到的接口
package com.wbz.springbootwebservice.service;
import javax.jws.WebService;
/**
* create by :Wangbz
* Date : 2020/12/15 15:17
**/
@WebService(name = "DemoService" ,targetNamespace = "http://service.springbootwebservice.wbz.com")
public interface DemoService {
public String sayHello(String user);
}
实现类
package com.wbz.springbootwebservice.service.impl;
import com.wbz.springbootwebservice.service.DemoService;
import javax.jws.WebService;
import java.util.Date;
/**
* create by :Wangbz
* Date : 2020/12/15 15:21
**/
@WebService(serviceName = "DemoService",
targetNamespace = "http://service.springbootwebservice.wbz.com",
endpointInterface = "com.wbz.springbootwebservice.service.DemoService")
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String user) {
return user + ", 现在时间 : " + "(" + new Date() + ")";
}
}
这里需要注意 接口和实现类的name需要相同,targetNamespace一般为包路径倒序。
配置类
package com.wbz.springbootwebservice.config;
import com.wbz.springbootwebservice.service.DemoService;
import com.wbz.springbootwebservice.service.impl.DemoServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
/**
* create by :Wangbz
* Date : 2020/12/15 15:27
**/
@Configuration
public class CxfConfig {
@Bean
public ServletRegistrationBean dispatcherServlet(){
return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus(){
return new SpringBus();
}
@Bean
public DemoService demoService(){
return new DemoServiceImpl();
}
@Bean
public Endpoint endpoint(){
EndpointImpl endpoint = new EndpointImpl(springBus(),demoService());
endpoint.publish("/api");
return endpoint;
}
}
启动类没什么特别的,就不往上粘了。这几个类写好就可以直接启动springboot。
http://localhost:8080/webservice/api?wsdl
访问路径:http://ip:端口/配置类中的路径?wsdl
最后感谢网上各位技术大佬的分享。