[webservice] springboot整合cxf

1. cxf是什么

    Web Service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的交互操作的应用程序。Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。Web Service是自描述、 自包含的可用网络模块, 可以执行具体的业务功能。Web Service也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XML、HTTP。Web Service减少了应用接口的花费。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。
    CXF,Apache CXF = Celtix +XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF,继承了 Celtix 和 XFire 两大开源项目的精华,提供了对JAX-WS全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。

2. springboot整合cxf的操作(server端发布)

2.1 pom.xml 中添加依赖

springboot整合Apache cxf

       <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
                <version>3.5.7</version>
                <exclusions><!-- CXF uses java.util.logging by default -->
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web-services</artifactId>
                <version>2.3.12.RELEASE</version>
                <exclusions>
                    <exclusion>
                        <groupId>com.sun.xml.bind</groupId>
                        <artifactId>jaxb-impl</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>


            <dependency>
                <groupId>org.dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>2.1.3</version>
            </dependency>
            <!-- dom4j解析xpath依赖 -->
            <dependency>
                <groupId>jaxen</groupId>
                <artifactId>jaxen</artifactId>
                <version>2.0.0</version>
            </dependency>

2.2 编写server端

@Service
// 配置targetNamespace和服务名称
@WebService(targetNamespace = "ws.yening.gitee.com", name = "MedicalOrderSvc")
public class MedicalOrderService {
   // 配置service中的method
    @WebMethod(operationName = "medicationOrderRequest",action="/medicationOrderRequest")
    @WebResult
    // 使用WebParam配置参数名称,不配置的话默认会是arg0之类的名称
    public R medicationOrderRequest(@WebParam(name = "message") String message) { 
      return null;
    }
}

2.3 service发布(cxf配置)

@Configuration
@EnableWs
public class CxfConfig {


    /**
     * 注意:此方法被注释后WSDL访问地址是http://127.0.0.1:8080/services/helloService?wsdl
     * 放开注释后WSDL访问地址是:http://127.0.0.1:8080/ws/helloService?wsdl
     */
    @Bean
    public ServletRegistrationBean wsServletConfig() {
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
    }

    @Resource
    private MedicalOrderService medicalOrderService;
    // 对于多个发布的service,可以写多个bean来实现
    @Bean
    public Endpoint orderEndpoint() {
        return formatEndpoint("/medicalOrderService", medicalOrderService);
    }


    /**
     * 格式化暴漏的ws接口信息
     *
     * @param path  暴露的ws请求地址
     * @param svc   与地址对应的ws服务
     * @return
     */
    @Resource
    Bus bus;

    private Endpoint formatEndpoint(String path, Object svc) {
        EndpointImpl endpoint = new EndpointImpl(bus, svc);
        endpoint.publish(path);
        return endpoint;
    }
}

发布完成后启动web服务,可以通过:http://127.0.0.1:8080/ws 访问已经发布的服务。
在这里分享一个天气预报的webservice服务:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

3. webservice使用(client端调用)

3.1 使用wsdl2java生成webservice客户端代码

将目录切换到下载的apache-cxf-版本文件包的bin目录下,然后执行如下命令wsdl2java -p com.vh -d /Users/johnny/Desktop/aa -client http://127.0.0.1:8083/ws/medicalOrderService?wsdl

  • -p:指定生成的Java代码的package
  • -d:指定生产的文件的根目录
  • -client 指定线上或本地的wsdl文件,用于生成客户端代码

3.2 使用

   @DisplayName("临时测试使用")
    @Test
    public void test1() {
        MedicalOrderServiceService svc = new MedicalOrderServiceService();
        svc.getMedicalOrderSvcPort().medicationOrderRequest(message);
    }

3.3 xpath说明

对于XML文档的读取来说,xpath几乎是通常的选择,而在使用webservice时通常伴随着对于XML文档的解析。
xpath常用规则(其它规则可以参考文档:https://blog.csdn.net/qq_44619675/article/details/113938171)

表达式描述
nodename选取此节点的所有子节点
/从根节点选取直接子节点(相当于绝对路径)
//从当前节点选择子节点(相当于相对路径)
.选取当前节点
选取当前节点的父节点
@选取属性
以上面天气预报为例使用dom4j+xpath读取第一个方法的描述
@Test
    @DisplayName("读取天气预报wsdl第一个方法的描述")
    public void test2() throws IOException, DocumentException {
        URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");
        URLConnection conn = url.openConnection();
        SAXReader saxReader = new SAXReader();
        Document doc = saxReader.read(conn.getInputStream());
        Node node = doc.selectSingleNode("/wsdl:definitions/wsdl:portType[@name=\"WeatherWebServiceSoap\"]/wsdl:operation[@name=\"getSupportCity\"]/wsdl:documentation");
        System.out.println(node.getText());
    }

4. 参考文档

  • https://cxf.apache.org/docs/index.html
  • https://baike.baidu.com/item/Web%20Service/1215039?fr=ge_ala
  • https://blog.csdn.net/qq_44619675/article/details/113938171
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值