1.添加相关依赖
在SpringBoot骨架工程的基础上,导入以下Jar包
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId><version>2.5.2</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.4.3</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.4.3</version></dependency>
2、创建服务端接口
@WebService(name = "DemoService", // 暴露服务名称targetNamespace = "http://service.webserviceprovider.lwq.com") // 命名空间,一般是接口的包名倒序public interface DemoService {public String sayHello(@WebParam(name = "name") String name);}
3、创建接口实现类
@Component@WebService(name = "Demoservice", // 与接口中指定的name一致targetNamespace = "http://service.webserviceprovider.lwq.com", // 与接口中的命名空间一致,一般是接口的包名倒序endpointInterface = "com.lwq.webserviceprovider.service.DemoService") // 接口地址public class DemoServiceImpl implements DemoService {@Overridepublic String sayHello(String name) {return "你好:" + name;}}
4、创建CXF配置类
@Configurationpublic class CxfConfig {@Beanpublic ServletRegistrationBean createServletRegistrationBean() {return new ServletRegistrationBean(new CXFServlet(), "/demo/*");}@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}@Beanpublic DemoService demoService() {return new DemoServiceImpl();}@Beanpublic Endpoint endpoint() {EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());endpoint.publish("/api");return endpoint;}}
5、启动并测试
启动服务端,并用SoapUI访问 http://localhost:8080/demo/api?wsdl,找到sayHello方法,输入参数,查看结果。

