ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

WebService学习(二)SpringBoot集成CXF实现WebService

2021-03-03 17:02:08  阅读:361  来源: 互联网

标签:SpringBoot CXFWebService name CXF cxf cxfWebService public WebService


CXF实现WebService

Apache CXF介绍

Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。

Apache CXF特点

  • 灵活部署
    轻量级容器:可在 Tomcat 或基于 Spring 的容器中部署 Services;集成 JBI:可以在如 ServiceMix, OpenESB or Petals 等等的 JBI 容器中将它部署为一个服务引擎;集成 SCA:可以部署在如 Tuscany 之类的 SCA 容器中;集成 J2EE:可以在 J2EE 应用服务器中部署 Services,比如:Geronimo、JOnAS、JBoss、WebSphere Application Server 和 WebLogic Application Server,以及 Jetty 和 Tomcat;独立的 Java 客户端/服务器。

  • 支持多种编程语言
    全面支持 JAX-WS 2.0 客户端/服务器编程模型;支持 JAX-WS 2.0 synchronous、asynchronous 和 one-way API’s;支持 JAX-WS 2.0 Dynamic Invocation Interface (DII) API;支持 wrapped and non-wrapped 风格;支持 XML messaging API;支持 JavaScript 和 ECMAScript 4 XML (E4X) ,客户端与服务端均支持;通过 Yoko 支持 CORBA;通过 Tuscany 支持 SCA;通过 ServiceMix 支持 JBI ;

  • 代码生成
    Java to WSDL;WSDL to Java;XSD to WSDL;WSDL to XML;WSDL to SOAP;WSDL to Service;
    CXF 框架支撑环境
    CXF 框架是一种基于 Servlet 技术的 SOA 应用开发框架,要正常运行基于 CXF 应用框架开发的企业应用,除了 CXF 框架本身之外,还需要 JDK 和 Servlet 容器的支持。

服务端搭建步骤

添加maven依赖

<properties>
   <java.version>1.8</java.version>
   <cxf.version>3.4.2</cxf.version>
</properties>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-core</artifactId>
    <version>${cxf.version}</version>
</dependency>

<dependency>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-rt-frontend-jaxws</artifactId>
   <version>${cxf.version}</version>
</dependency>

<dependency>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-rt-transports-http-jetty</artifactId>
   <version>${cxf.version}</version>
</dependency>            

编写接口服务类和实现类

  • 接口类
/**
 * name: 暴露服务名称
 * targetNamespace: 命名空间,一般是接口的包名倒序,这两个可以不设置,有默认值的
 *
 */
@WebService(name = "cxfWebService", targetNamespace = "http://service.cxfdemo.fengfan.com")
public interface CXFWebService {

    @WebMethod(operationName = "hello")
    @WebResult
    String hello(@WebParam(name = "name") String name, @WebParam(name = "age") int age);

}
  • 服务实现类
/**
 * name: 与接口中指定的name一致
 * targetNamespace: 命名空间,与接口中的命名空间一致,一般是接口的包名倒序
 * endpointInterface:
 */
@WebService(serviceName = "cxfWebService", targetNamespace = "http://service.cxfdemo.fengfan.com", endpointInterface = "com.fengfan.cxfdemo.service.CXFWebService")
@Service
public class CXFWebServiceImpl  implements CXFWebService {

    @Override
    public String hello(String name, int age) {
        return "hello:姓名"+ name + "年龄" + age;
    }
}

服务发布配置

@Configuration
public class WebserviceCoinfig {
    @Resource
    private CXFWebService cxfWebService;

    @Bean
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/services/*");
    }

    @Bean
    public SpringBus cxf() {
        return new SpringBus();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(cxf(), cxfWebService);
        endpoint.publish("/cxfWebService");
        return endpoint;
    }
}

效果展示

在这里插入图片描述

客户端实现

class CXFWebServiceImplTest {

    /**
     * 方式1 代理类工厂方式
     */

    @Test
    void hello1 (){
        try {
            // 接口地址
            String address = "http://127.0.0.1:8080/cxfDemo/services/cxfWebService?wsdl";
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(CXFWebService.class);
            // 创建一个代理接口实现
            CXFWebService cxfWebService = (CXFWebService) jaxWsProxyFactoryBean.create();
            // 调用代理接口的方法调用并返回结果
            String result = cxfWebService.hello("张三", 1);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 方式2 动态调用
     */
    @Test
    void hello2() {
        JaxWsDynamicClientFactory jaxWsDynamicClientFactory = JaxWsDynamicClientFactory.newInstance();
        try {
            Client client = jaxWsDynamicClientFactory.createClient("http://127.0.0.1:8080/cxfDemo/services/cxfWebService?wsdl");
            Object[] objects = client.invoke("hello", "张三", 1);
            System.out.println(objects[0].toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

效果
在这里插入图片描述

项目地址

链接: https://gitee.com/fengerwa/webservice-study.

标签:SpringBoot,CXFWebService,name,CXF,cxf,cxfWebService,public,WebService
来源: https://blog.csdn.net/weixin_43869563/article/details/114306991

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有