【WebService】springboot整合cxf实现webService远程调用

【WebService】springboot整合cxf实现webService远程调用,第1张

概述一、前言         在做接口对接的时候需要接口以HTTPS和WebService的形式暴露,虽然最近这几年restful非常火爆,但在一些特定的领域或者一些老旧系统中仍然采用WebService的形式来实现远程通信。        上一篇博客讲了WebService实现远程调用,在博文的最后,自己阐述了关于那种客户端调用存在的问题,如大家有疑问,欢迎留言区指正。 二、实现        废话

一、前言

        在做接口对接的时候需要接口以httpS和WebService的形式暴露,虽然最近这几年restful非常火爆,但在一些特定的领域或者一些老旧系统中仍然采用WebService的形式来实现远程通信。

       上一篇博客讲了WebService实现远程调用,在博文的最后,自己阐述了关于那种客户端调用存在的问题,如大家有疑问,欢迎留言区指正。

二、实现

       废话上一篇博客已经说得差不多了。直接上代码吧。

1、首先pom文件中添加如下依赖

<!-- CXF webservice -->		<dependency>			<groupID>org.apache.cxf</groupID>			<artifactID>cxf-spring-boot-starter-jaxws</artifactID>			<version>3.1.11</version>		</dependency>		<!-- CXF webservice -->

2、服务接口

package com.zero.service;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;@WebService(name="CommonService",//暴露服务名称        targetnamespace = "http://service.zero.com/") //命名空间,一般是接口的倒叙public interface CommonService {    @WebMethod    @WebResult(name="String",targetnamespace = "")    public String sayHello(@WebParam(name="username")String name);}

3、接口实现

package com.zero.service.impl;import com.zero.service.CommonService;import org.springframework.stereotype.Component;import javax.jws.WebService;@WebService(servicename = "CommonService",//与接口中指定的name一直        targetnamespace = "http://service.zero.com/",// 与接口中的命名空间一致,一般是接口的包名倒        endpointInterface = "com.zero.service.CommonService"//接口地址)@Componentpublic class CommonServiceImpl implements CommonService {    @OverrIDe    public String sayHello(String name) {        return "Hello,"+name;    }}

4、cxf发布WebService服务

     默认服务在ip:port/services/**路径下面

package com.zero.config;import com.zero.service.CommonService;import org.apache.cxf.Bus;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;@Configurationpublic class CxfConfig {    @autowired(required = false)    private Bus bus;    @autowired(required = false)    CommonService commonService;    @Bean    public Endpoint endpoint(){        EndpointImpl endpoint=new EndpointImpl(bus,commonService);        endpoint.publish("/CommonService");        return endpoint;    }}

5、cxf调用WebService接口

package com.zero.service;import org.apache.cxf.endpoint.ClIEnt;import org.apache.cxf.jaxws.JaxWsProxyfactorybean;import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicclIEntFactory;public class CxfClIEnt {    public static voID main(String[] args) {        cl2();    }    /**     * 方式1.代理类工厂的方式,需要拿到对方的接口     */    public static voID cl1() {        try {            // 接口地址            String address = "http://localhost:8089/services/CommonService?wsdl";            // 代理工厂            JaxWsProxyfactorybean jaxWsProxyfactorybean = new JaxWsProxyfactorybean();            // 设置代理地址            jaxWsProxyfactorybean.setAddress(address);            // 设置接口类型            jaxWsProxyfactorybean.setServiceClass(CommonService.class);            // 创建一个代理接口实现            CommonService cs = (CommonService) jaxWsProxyfactorybean.create();            // 数据准备            String username = "LemmonTree";            // 调用代理接口的方法调用并返回结果            String result = cs.sayHello(username);            System.out.println("返回结果:" + result);        } catch (Exception e) {            e.printstacktrace();        }    }    /**     * 动态调用方式     */    public static voID cl2() {        // 创建动态客户端        JaxWsDynamicclIEntFactory dcf = JaxWsDynamicclIEntFactory.newInstance();        ClIEnt clIEnt = dcf.createClIEnt("http://localhost:8089/services/CommonService?wsdl");        // 需要密码的情况需要加上用户名和密码        // clIEnt.getoutInterceptors().add(new ClIEntLoginInterceptor(USER_name,// PASS_WORD));        Object[] objects = new Object[0];        try {            // invoke("方法名",参数1,参数2,参数3....);            objects = clIEnt.invoke("sayHello","LemmonTree");            System.out.println("返回数据:" + objects[0]);        } catch (java.lang.Exception e) {            e.printstacktrace();        }    }}

三、总结

       本文并没有使用wsdl文档生成java类,而是采用cxf完成服务端的发布和客户端的调用。

总结

以上是内存溢出为你收集整理的【WebService】springboot整合cxf实现webService远程调用全部内容,希望文章能够帮你解决【WebService】springboot整合cxf实现webService远程调用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1217192.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存