Spring-boot整合Webservice服务端
Spring Boot整合Webservice服务端
本文是基于前辈一顿吃不饱的文章SpringBoot整合WebService(服务端+客户端)-CSDN博客,由于工作需要用.NET调用其他系统发布的WebService服务,尝试用java搭建一个WebService服务端测试一下,在此记录。
- pom.xml
注意springboot版本和CXF可能会存在版本不兼容
· Spring Boot 3.x:使用CXF的Jakarta版本
· Spring Boot 2.x:使用CXF的Java EE版本
本文是基于Spring Boot 2.7.9参考博主构建的,完整依赖如下:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.9 org.cqw WebService 0.0.1-SNAPSHOT WebService WebService 17 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.projectlombok lombok true org.springframework.boot spring-boot-starter-web-services org.apache.cxf cxf-rt-frontend-jaxws 3.2.0 org.apache.cxf cxf-rt-transports-http 3.2.0 org.apache.cxf cxf-core 3.3.5 org.apache.cxf cxf-rt-transports-http 3.2.4 org.springframework.boot spring-boot-maven-plugin
- 创建WebService接口
package org.cqw.webservice.service; import javax.jws.WebService; @WebService(name = "UserService", // 暴露服务名称 targetNamespace = "http://service.webservice.cqw.org"// 命名空间,一般是接口的包名倒序 ) public interface UserService { String testWebService(); }
- 创建接口实现类
package org.cqw.webservice.service; import javax.jws.WebService; @WebService(serviceName = "UserService", // 与接口中@WebService指定的name一致 targetNamespace = "http://service.webservice.cqw.org", // 与接口中的命名空间一致,一般是接口的包名倒序 endpointInterface = "org.cqw.webservice.service.UserService"// 接口地址 ) @Slf4j public class UserServiceImpl implements UserService { public String testWebService() { log.info("我被调用了"); return "Hello"; } }
- 在项目路径(本文是/src/main/java/org/cqw)下创建config文件夹,创建配置类
package org.cqw.webservice.config; 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.cqw.webservice.service.UserService; import org.cqw.webservice.service.UserServiceImpl; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.xml.ws.Endpoint; @Configuration public class StartClas { @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean(name = "wsBean") public ServletRegistrationBean dispatcherServlet() { ServletRegistrationBean wbsServlet = new ServletRegistrationBean(new CXFServlet(), "/ws/*"); return wbsServlet; } @Bean public UserService userService() { return new UserServiceImpl(); } @Bean public Endpoint endpointPurchase(SpringBus springBus, UserService userService) { EndpointImpl endpoint = new EndpointImpl(springBus(), userService()); endpoint.publish("/api"); log.info("服务发布成功!地址为:http://localhost:8081/ws/api?wsdl"); return endpoint; } }
浏览器访问:http://localhost:8080/ws/api?wsdl
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。