• Apache CXF使用Jetty发布WebService


    一、概述

    Apache CXF提供了用于方便地构建和开发WebService的可靠基础架构。它允许创建高性能和可扩展的服务,可以部署在Tomcat和基于Spring的轻量级容器中,也可以部署在更高级的服务器上,例如Jboss、WebSphere或WebLogic。 CXF提供了以下功能:

    • WebService服务标准支持:

    Java API for XML Web Services (JAX-WS)
    SOAP
    WebService描述语言(Web Services Description Language ,WSDL)
    消息传输优化机制(Message Transmission Optimization Mechanism,MTOM)
    WS-Basic Profile
    WS-Addressing
    WS-Policy
    WS-ReliableMessaging
    WS-Security

    • 前端建模:CXF允许使用不同的前端API来创建Service。如CXF允许使用简单的工厂Bean并通过JAX-WS实现来创建WebService,允许创建动态WebService客户端。
    • 工具支持:CXF提供了在Java Bean、WebService和WSDL之间进行转换的工具,提供了对Maven和Ant集成的支持,并无缝地支持Spring集成。
    • RESTful支持:CXF支持Restful,并支持Java平台的JAX-RS实现。
    • 对不同传输和绑定的支持:CXF支持不同数据类型的传输,除了支持SOAP和HTTP协议绑定外,还支持JAXB和AEGIS绑定。
    • 对非XML绑定的支持:CXF支持非XML绑定,如JSON、CORBA、JBI和SCA等。
    • Code First和Xml First:CXF支持使用Code First或者Xml First的方式创建WebService。

    二、使用CXF内置jetty发布WebService

    maven:

    <properties>  
            <cxf.version>3.1.4</cxf.version>  
    </properties>
    
    <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</artifactId>  
            <version>${cxf.version}</version>  
    </dependency>  
              
              
    <!-- 使用cxf内置的jetty服务器发布WebService -->  
    <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-transports-http-jetty</artifactId>  
            <version>${cxf.version}</version>  
    </dependency> 

    ---

    接口:

    @WebService
    public interface HelloService{
    
        public String helloCxf();
    
        public String hello(String name);public User getUser(int id);
    
        public void saveUser(User user);
    
    }

    实现:

    @WebService(serviceName = "helloService",
            endpointInterface = "cn.lg.ws.hellocxf.HelloService"
    )
    public class HelloServiceImpl implements HelloService{
    
        @Override
        public String helloCxf(){
            return "Hello CXF!";
        }
    
        @Override
        public String hello(String name)
        {
            return "Hello " + name;
        }
    
        @Override
        public User getUser(int id) {
            User u1 = new User();
            return u1;
        }
    
        @Override
        public void saveUser(User user) {
            System.out.println(user.toString());
        }
    
    }

    发布:

    public class PublishService{
        /**
         * 使用CXF的JaxWsServerFactoryBean发布服务
         * @param
         */
        public static void main(String[] args) {
            JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
            factory.setServiceClass(HelloService.class);
            //服务发布地址
            factory.setAddress("http://localhost:8088/soap/hello");
            factory.setServiceBean(new HelloServiceImpl());
            factory.create();
            System.out.println("publish success");
        }
    }

    使用浏览器访问 http://localhost:8088/soap/hello?wsdl 可以看到wsdl如下,则说明发布成功

    使用CXF在客户端调用WebService:

    public class ClientTest{
        public static void main(String[] args) {
            JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean();
            jwpfb.setServiceClass(HelloService.class);
            jwpfb.setAddress("http://localhost:8088/sop/hello");
            HelloService hs = (HelloService) jwpfb.create();
            System.out.println(hs.getUser(101));
            Q.p(hs.hello("luangeng"));
        }
    }

    ---

    相关类可通过以下命令产生:

    wsimport -p com.ickes.cxf.client -keep http://localhost:8088/sop/hello?wsdl

  • 相关阅读:
    同步 异步 阻塞 非阻塞深入理解
    TCP的三次握手四次挥手
    dom事件
    Vue 无痕 刷新页面
    事件冒泡 捕获 事件流
    gulp
    懒加载
    Object.prototype.toString.call() 判断某个对象属于哪种内置类型 type instanceof Object.prototype.toString.call 区别
    css sass less
    node module.exports exports
  • 原文地址:https://www.cnblogs.com/luangeng/p/6575061.html
Copyright © 2020-2023  润新知