使用 SpringBoot 发布一个 WebService 很简单,主要分为三步:

1、添加依赖

<!-- WebService --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId></dependency><!-- CXF webservice --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId></dependency><!-- CXF webservice -->

2、编写主配置类


import com.shanhy.muses.example.wsservice.DemoWebService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.xml.ws.Endpoint;/*** 注意:* org.apache.cxf.Bus* org.apache.cxf.bus.spring.SpringBus* org.apache.cxf.jaxws.EndpointImpl* javax.xml.ws.Endpoint** @author shanhy* @date 2020/11/19 20:38*/
@Configuration
public class WebServiceConfiguration {@Autowiredprivate DemoWebService demoWebService;/*** Apache CXF 核心架构是以BUS为核心,整合其他组件。* Bus是CXF的主干, 为共享资源提供一个可配置的场所,作用类似于Spring的ApplicationContext,这些共享资源包括* WSDl管理器、绑定工厂等。通过对BUS进行扩展,可以方便地容纳自己的资源,或者替换现有的资源。默认Bus实现基于Spring架构,* 通过依赖注入,在运行时将组件串联起来。BusFactory负责Bus的创建。默认的BusFactory是SpringBusFactory,对应于默认* 的Bus实现。在构造过程中,SpringBusFactory会搜索META-INF/cxf(包含在 CXF 的jar中)下的所有bean配置文件。* 根据这些配置文件构建一个ApplicationContext。开发者也可以提供自己的配置文件来定制Bus。*/@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}/*** 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问* 此方法被注释后, 即不改变前缀名(默认是services), wsdl访问地址为 http://127.0.0.1:8080/services/ws/api?wsdl* 去掉注释后wsdl访问地址为:http://127.0.0.1:8080/soap/ws/api?wsdl* http://127.0.0.1:8080/soap/列出服务列表 或 http://127.0.0.1:8080/soap/ws/api?wsdl 查看实际的服务* 新建Servlet记得需要在启动类添加注解:@ServletComponentScan* <p>* 如果启动时出现错误:not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet* 可能是springboot与cfx版本不兼容。* 同时在spring boot2.0.6之后的版本与xcf集成,不需要在定义以下方法,直接在application.properties配置文件中添加:* cxf.path=/service(默认是services)*///@Bean//public ServletRegistrationBean dispatcherServlet() {//    return new ServletRegistrationBean(new CXFServlet(), "/soap/*");//}@Beanpublic Endpoint endpoint() {Endpoint endpoint = new EndpointImpl(springBus(), demoWebService);endpoint.publish("/ws/demoWsApi");return endpoint;}
}

3、编写WebService具体实现

接口定义:

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import java.util.HashMap;
import java.util.Map;/*** @author shanhy* @date 2020/11/19 20:27*/
@WebService(name = DemoWebService.WEBSERVICE_NAME, targetNamespace = DemoWebService.TARGET_NAMESPACE)
public interface DemoWebService {String WEBSERVICE_NAME = "DemoWebService";String TARGET_NAMESPACE = "http://demo.webservice.shanhy.com";String WEBSERVICE_INTERFACE_NAME = "com.shanhy.muses.example.wsservice.DemoWebService";@WebMethodString hello(@WebParam(name = "userName3") String name);@WebMethodString test(@WebParam(name="headerMap") HashMap<String, String> headerMap, @WebParam HashMap<String, String> parameterMap, @WebParam String bodyJson);}

接口实现类:


import com.shanhy.muses.example.wsservice.DemoWebService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;import javax.jws.WebService;
import java.util.HashMap;/*** WebService涉及到的有这些 "四解三类 ", 即四个注解,三个类** @author shanhy* @WebMethod* @WebService* @WebResult* @WebParam SpringBus* Endpoint* EndpointImpl* <p>* 一般我们都会写一个接口,然后再写一个实现接口的实现类,但是这不是强制性的* @WebService 注解表明是一个webservice服务。* name:对外发布的服务名, 对应于<wsdl:portType name="ServerServiceDemo"></wsdl:portType>* targetNamespace:命名空间,一般是接口的包名倒序, 实现类与接口类的这个配置一定要一致这种错误* Exception in thread "main" org.apache.cxf.common.i18n.UncheckedException: No operation was found with the name xxxx* 对应于targetNamespace="http://server.webservice.example.com"* endpointInterface:服务接口全路径(如果是没有接口,直接写实现类的,该属性不用配置), 指定做SEI(Service EndPoint Interface)服务端点接口* serviceName:对应于<wsdl:service name="ServerServiceDemoImplService"></wsdl:service>* portName:对应于<wsdl:port binding="tns:ServerServiceDemoImplServiceSoapBinding" name="ServerServiceDemoPort"></wsdl:port>* @WebMethod 表示暴露的服务方法, 这里有接口ServerServiceDemo存在,在接口方法已加上@WebMethod, 所以在实现类中不用再加上,否则就要加上* operationName: 接口的方法名* action: 没发现又什么用处* exclude: 默认是false, 用于阻止将某一继承方法公开为web服务* @WebResult 表示方法的返回值* name:返回值的名称* partName:* targetNamespace:* header: 默认是false, 是否将参数放到头信息中,用于保护参数,默认在body中* @WebParam name:接口的参数* partName:* targetNamespace:* header: 默认是false, 是否将参数放到头信息中,用于保护参数,默认在body中* model:WebParam.Mode.IN/OUT/INOUT* @date 2020/11/19 20:29*/
@Component
@WebService(name = DemoWebService.WEBSERVICE_NAME, targetNamespace = DemoWebService.TARGET_NAMESPACE,endpointInterface = DemoWebService.WEBSERVICE_INTERFACE_NAME)
public class DemoWebServiceImpl implements DemoWebService {@Overridepublic String hello(String userName) {if (StringUtils.isBlank(userName)) {return "传入的name为空";}return "Hello: ".concat(userName);}@Overridepublic String test(HashMap<String, String> headerMap, HashMap<String, String> parameterMap, String bodyJson) {return "mapParameters";}
}

(END)

SpringBoot 发布 WebService相关推荐

  1. idea springboot 发布webservice 发布服务_阿里云发布 Spring Boot 新脚手架,真香

    作者 | 良名  阿里巴巴技术专家 背景 相信很多人都使用过 start.spring.io 来初始化自己的 Spring Boot 工程,这个工具为开发者提供了丰富的可选组件,并且可以选择多种打包方 ...

  2. idea springboot 发布webservice 发布服务_太赞了:Spring boot+redis实现消息发布与订阅...

    一.创建spring boot项目 org.springframework.boot spring-boot-starter-data-redis org.springframework.boot s ...

  3. SpringBoot+CXF发布Webservice时报错:counts of IllegalAnnotationExceptions

    由于项目需要,与生成环境的其他系统对接,对方采用的是Webservice的接口方式,为了验证我们自己开发的对接是否正常,因此,自己写了对应的接口来验证,结果发布Webservice的时候报错: Cau ...

  4. SpringBoot整合WebService

    WebService是一种传统的SOA技术架构,它不依赖于任何的编程语言,也不依赖于任何的技术平台,可以直接基于HTTP协议实现网络应用间的数据交互. 面向服务架构(SOA)是一个组件模型,它将应用程 ...

  5. SpringBoot整合WebService(服务端+客户端)

    SpringBoot整合WebService(服务端+客户端) 文章目录 SpringBoot整合WebService(服务端+客户端) 一.服务端 1.项目结构 2.创建好SpringBoot项目后 ...

  6. WebService入门教程(服务端发布WebService)

    本篇内容过多,时间紧迫的朋友可以通过目录快速筛选自己想要看的内容,本人接触webservice也没多久,也处于学习阶段,如果有错误请指正,如果已经是大神请略过这篇文章,这篇文章不涉及webservic ...

  7. 使用Apache cxf 和Spring在Tomcat下发布Webservice指南

    转载 http://blog.csdn.net/zhangzhaokun/article/details/4750021 最近学习了如何使用apache cxf和Spring发布webservice, ...

  8. Spring和CXF整合发布WebService(服务端、客户端)

    参考Spring和CXF整合发布WebService(服务端.客户端) 转载于:https://www.cnblogs.com/timspace/p/11113576.html

  9. WebService大讲堂之Axis2(3):使用services.xml文件发布WebService

    用Axis2 实现Web Service ,虽然可以将POJO 类放在axis2\WEB-INF\pojo 目录中直接发布成Web Service ,这样做不需要进行任何配置,但这些POJO 类不能在 ...

最新文章

  1. c++区块链实例_确定技术任务了解区块链限制 开发区块链你该做哪些准备工作?...
  2. Python Django CBV下的通用视图函数
  3. bgl 词典_器材屋 篇五十二:“哪里不会点哪里”的后时代——哪里不识扫哪里:科大讯飞扫描词典笔评测_点读机...
  4. 计算机英语应用研究,计算机英语辅助学习系统的研究与应用-软件工程专业论文.docx...
  5. Bitcoin 地址原理(1)Base58编码
  6. 2017.9.17 kamp 思考记录
  7. 处女篇:ObjectDataSource+CodeSmith实现基础增删改查功能
  8. 汇编学习--7.11--内存寻址
  9. ERROR: Could not create or update '/usr/local/nagios/var/nagios.configtest'
  10. Android的 线性布局,Android布局之LinearLayout线性布局
  11. Atitit.变量的定义 获取 储存 物理结构 基本类型简化 隐式转换 类型推导 与底层原理 attilaxDSL
  12. SQL Server数据库优化的几种方法.
  13. 数学建模之蒙特卡洛算法
  14. oCPC和oCPM的本质区别是什么?
  15. APS54085 外围电路简单_调光无频闪 智能家居照明
  16. 资本寒冬之下的聚美优品私有化
  17. 要「自我颠覆」的华为Mate 10来了
  18. js-xlsx,table_to_book导出数据取消自动加工
  19. HTML5的基础知识整理
  20. Windows On Top v3.8.0 Windows窗口置顶小工具单文件版

热门文章

  1. werkzeug 详解
  2. CADENCE Allegro反白丝印的制作
  3. cmd 系统找不到指定路径的问题(Python编程从入门到实践1.5.1踩坑)
  4. php手绘功能,Canvas的手绘风格图形库Rough.js-
  5. NLP学习D2-TF2基础学习-北大教程
  6. 6.1 文本情感倾向性分析
  7. hls之xfopencv
  8. 三星s系列和note系列记录
  9. 在Microsoft Edge兼容模式打开Internet Explorer站点(永久)
  10. Java寒假学习总结