如何通过SpringBoot整合CXF实现WebService调用?

2026-06-10 14:482阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计826个文字,预计阅读时间需要4分钟。

如何通过SpringBoot整合CXF实现WebService调用?

1. WebService简介 WebService是一种基于网络的分布式计算技术,允许不同平台和语言的应用程序之间进行互操作。它通过标准化的XML消息交换实现数据传输,支持跨网络、跨语言的集成。近年来,随着互联网的普及和云计算的发展,WebService在各个领域得到了广泛应用。下面简要介绍WebService的相关内容。

如何通过SpringBoot整合CXF实现WebService调用?

1、写在前面

WebService 对我来说既熟悉又陌生,已经将近六七年没有看到过他了, 具体的介绍我就不多少了, 想了解的百度百科下说的很详细。

之所以突然研究WebService是接到一个需求要去给 XX 项目做一个适配层,他们原有系统是使用webservice做的,所以……
那我们就来看看,这一个古老的技术如何和如今最流行的框架SpringBoot进行结合。

2、SpringBoot 集成WebService

2.1 导入依赖

compile('org.springframework.boot:spring-boot-starter-web-services', 'org.apache.cxf:cxf-spring-boot-starter-jaxws:3.2.5', 'org.apache.cxf:cxf-rt-frontend-jaxws:3.1.6', 'org.apache.cxf:cxf-rt-transports-36.101.208.59:8090/axis/services/bdcgzcxfw_wx") public interface WebService { /** * * @param wxid 微信ID * @param xm 姓名 * @param sfzh 身份证号 * @param sjh 手机号 * @param macId 预定用户 * @param password 密码 * @return 查询结果 Base64字符串 */ @WebMethod(operationName = "gzcxfw_hlw_wxrz_Info_cs") @WebResult(name = "gzcxfw_hlw_wxrz_Info_csReturn") String gzcxfwHlwWxrzInfoCs(@WebParam(name = "WXID", targetNamespace = "36.101.208.59:8090/axis/services/bdcgzcxfw_wx") String wxid, @WebParam(name = "XM") String xm, @WebParam(name = "SFZH", targetNamespace = "36.101.208.59:8090/axis/services/bdcgzcxfw_wx") String sfzh, @WebParam(name = "SJH") String sjh, @WebParam(name = "mac_id", targetNamespace = "36.101.208.59:8090/axis/services/bdcgzcxfw_wx") String macId, @WebParam(name = "password") String password );

2.3 实现类

/** * @author yueli * @date 2019-08-05 19:17 */ @javax.jws.WebService(serviceName = "gzcxfw_wx_webserviceService", targetNamespace = "36.101.208.59:8090/axis/services/bdcgzcxfw_wx") public class WebServiceImpl implements WebService { private static Logger logger = LoggerFactory.getLogger(CatalogInfoImpl.class); @Override public String gzcxfwHlwWxrzInfoCs(String wxid, String xm, String sfzh, String sjh, String macId, String password) { logger.info("gzcxfwHlwWxrzInfoCs: 入参- wxid:{}, xm:{}, sfzh:{}, sjh:{}, macId:{}, pawd:{}", wxid, xm, sfzh, macId, password); return wxid + “:” + sfzh; }

实现类就很简单了。和我们一般写的没啥区别,

2.4 发布

package com.tusdao.base.configuration; import com.tusdao.TusdaoWebserviceApplication; import com.tusdao.webservice.service.WebService; import com.tusdao.webservice.service.impl.WebServiceImpl; 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.ComponentScan; import org.springframework.context.annotation.Configuration; import javax.xml.ws.Endpoint; /** * @author yueli * @date 2019-08-05 19:24 */ @Configuration @ComponentScan(basePackageClasses = TusdaoWebserviceApplication.class) public class CxfConfig { @SuppressWarnings("all") @Bean(name = "cxfServletRegistration") public ServletRegistrationBean dispatcherServlet() { //创建服务并指定服务名称 return new ServletRegistrationBean(new CXFServlet(), "/axis/services/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public WebService webService() { return new WebServiceImpl(); } /** * 注册WebServiceDemoService接口到webservice服务 * * @return */ @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), webService()); endpoint.publish("/bdcgzcxfw_wx"); endpoint.getInInterceptors().add(new ServerNameSpaceInterceptor()); //endpoint.getInInterceptors().add(new InInterceptor()); return endpoint; } }

这个就简单了, 我们在使用时可以直接copy过去就行。

最后就是启动项目了。

启动后我们直接输入项目地址:localhost:8090/指定的服务名


会看到生成的ssdl。到这基本搭建就结束了。测试在这就不写了, 大家可以使用wsdl生成客户端,或者直接使用github.com/yuelicn/springboot-webservice-cxf
或者直接clone >> github.com/yuelicn/springboot-webservice-cxf

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计826个文字,预计阅读时间需要4分钟。

如何通过SpringBoot整合CXF实现WebService调用?

1. WebService简介 WebService是一种基于网络的分布式计算技术,允许不同平台和语言的应用程序之间进行互操作。它通过标准化的XML消息交换实现数据传输,支持跨网络、跨语言的集成。近年来,随着互联网的普及和云计算的发展,WebService在各个领域得到了广泛应用。下面简要介绍WebService的相关内容。

如何通过SpringBoot整合CXF实现WebService调用?

1、写在前面

WebService 对我来说既熟悉又陌生,已经将近六七年没有看到过他了, 具体的介绍我就不多少了, 想了解的百度百科下说的很详细。

之所以突然研究WebService是接到一个需求要去给 XX 项目做一个适配层,他们原有系统是使用webservice做的,所以……
那我们就来看看,这一个古老的技术如何和如今最流行的框架SpringBoot进行结合。

2、SpringBoot 集成WebService

2.1 导入依赖

compile('org.springframework.boot:spring-boot-starter-web-services', 'org.apache.cxf:cxf-spring-boot-starter-jaxws:3.2.5', 'org.apache.cxf:cxf-rt-frontend-jaxws:3.1.6', 'org.apache.cxf:cxf-rt-transports-36.101.208.59:8090/axis/services/bdcgzcxfw_wx") public interface WebService { /** * * @param wxid 微信ID * @param xm 姓名 * @param sfzh 身份证号 * @param sjh 手机号 * @param macId 预定用户 * @param password 密码 * @return 查询结果 Base64字符串 */ @WebMethod(operationName = "gzcxfw_hlw_wxrz_Info_cs") @WebResult(name = "gzcxfw_hlw_wxrz_Info_csReturn") String gzcxfwHlwWxrzInfoCs(@WebParam(name = "WXID", targetNamespace = "36.101.208.59:8090/axis/services/bdcgzcxfw_wx") String wxid, @WebParam(name = "XM") String xm, @WebParam(name = "SFZH", targetNamespace = "36.101.208.59:8090/axis/services/bdcgzcxfw_wx") String sfzh, @WebParam(name = "SJH") String sjh, @WebParam(name = "mac_id", targetNamespace = "36.101.208.59:8090/axis/services/bdcgzcxfw_wx") String macId, @WebParam(name = "password") String password );

2.3 实现类

/** * @author yueli * @date 2019-08-05 19:17 */ @javax.jws.WebService(serviceName = "gzcxfw_wx_webserviceService", targetNamespace = "36.101.208.59:8090/axis/services/bdcgzcxfw_wx") public class WebServiceImpl implements WebService { private static Logger logger = LoggerFactory.getLogger(CatalogInfoImpl.class); @Override public String gzcxfwHlwWxrzInfoCs(String wxid, String xm, String sfzh, String sjh, String macId, String password) { logger.info("gzcxfwHlwWxrzInfoCs: 入参- wxid:{}, xm:{}, sfzh:{}, sjh:{}, macId:{}, pawd:{}", wxid, xm, sfzh, macId, password); return wxid + “:” + sfzh; }

实现类就很简单了。和我们一般写的没啥区别,

2.4 发布

package com.tusdao.base.configuration; import com.tusdao.TusdaoWebserviceApplication; import com.tusdao.webservice.service.WebService; import com.tusdao.webservice.service.impl.WebServiceImpl; 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.ComponentScan; import org.springframework.context.annotation.Configuration; import javax.xml.ws.Endpoint; /** * @author yueli * @date 2019-08-05 19:24 */ @Configuration @ComponentScan(basePackageClasses = TusdaoWebserviceApplication.class) public class CxfConfig { @SuppressWarnings("all") @Bean(name = "cxfServletRegistration") public ServletRegistrationBean dispatcherServlet() { //创建服务并指定服务名称 return new ServletRegistrationBean(new CXFServlet(), "/axis/services/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public WebService webService() { return new WebServiceImpl(); } /** * 注册WebServiceDemoService接口到webservice服务 * * @return */ @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), webService()); endpoint.publish("/bdcgzcxfw_wx"); endpoint.getInInterceptors().add(new ServerNameSpaceInterceptor()); //endpoint.getInInterceptors().add(new InInterceptor()); return endpoint; } }

这个就简单了, 我们在使用时可以直接copy过去就行。

最后就是启动项目了。

启动后我们直接输入项目地址:localhost:8090/指定的服务名


会看到生成的ssdl。到这基本搭建就结束了。测试在这就不写了, 大家可以使用wsdl生成客户端,或者直接使用github.com/yuelicn/springboot-webservice-cxf
或者直接clone >> github.com/yuelicn/springboot-webservice-cxf

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。