zoukankan      html  css  js  c++  java
  • WebService

    https://www.cnblogs.com/h-c-g/p/10882935.html

    较好的

    https://www.cnblogs.com/h-c-g/p/10882935.html

    3.1 Apahce CXF

      Apache CXF=Celtix+XFfIRE,ApacheCXF的前称就叫Apace Celtixfire,现在已经正式更名为 Apahce CXF了,以下简称CXF,cxf继承了Celtix和XFire两大开源项目的精华,提供了对JAX-WS全面的支持,并且提供了多种Binding、DataBing、Transport以及各种Formt的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者WSDL优先(WSDL First)来轻松的实现web_Services发布和使用。目前它仍只是一个孵化项目。

      Apahce CXF 是一个开源的Services框架,CXF帮助您利用Frontend编程API来构建和开发Services,像jax-ws.这些services可以支持多种协议,比如:soap、xml/http、restfulhttp或者corba,并且可以在多种传输协议上运行,比如:http、jms或者jbi,cxf大大简化了services的创建,同时它继承了xfire传统,一样可以天然地和spring进行无缝集成。

    3.2功能特性

    cxf包含了大量的功能特性,但是主要集中在以下这几个方面:

      (1)支持web services标准:cxf支持多种web service标准,包含soap、basic proifle、ws-addressing、ws-policy、ws-ReliableMessaging和WS-Securiy。

      (2)Frontends:CXF支持多种"frontend"编程模型,cxf实现了jax-ws api(遵循jax-ws2.0tck版本),它也包含一个simple frontend允许客户端和EndPOint的创建,而不是需要Annotation注解。cxf既支持wsdl优先开发,也支持从java的代码优先开发模式。

      (3)容易使用: cxf设计的更加直观与容易使用。有大量简单的api来快速的构建代码优先的services,各种maven的插件也使集成更加容易,支持jax-ws api,支持Spirng2.0更加简化的xml配置方式,等等。

      (4)支持二进制和遗留协议;cxf的设计即使一种可以插播的架构,既可以支持xml

    3.3

    pring boot整合cxf发布webservice服务和cxf客户端调用
    本案例使用maven方式
    核显文件清单
    1.pom.xml

    复制代码
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
     
        <groupId>com.leftso</groupId>
        <artifactId>demo-webservice-cxf</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
     
        <name>demo-webservice-cxf</name>
        <description>Demo project for Spring Boot security</description>
     
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.5.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
     
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
     
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
     
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
     
            <!-- CXF webservice -->
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
                <version>3.1.11</version>
            </dependency>
            <!-- CXF webservice -->
     
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
     
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
     
     
    </project>
    复制代码


    2.CommonService.java 服务接口

    复制代码
    package com.leftso.webservice;
     
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebResult;
    import javax.jws.WebService;
     
    /**
     * 接口
     * 
     * @author leftso
     *
     */
    @WebService(name = "CommonService", // 暴露服务名称
            targetNamespace = "http://webservice.leftso.com/"// 命名空间,一般是接口的包名倒序
    )
    public interface CommonService {
        @WebMethod
        @WebResult(name = "String", targetNamespace = "")
        public String sayHello(@WebParam(name = "userName") String name);
     
    }
    复制代码

    3.接口实现

    复制代码
    package com.leftso.webservice;
     
    import javax.jws.WebService;
     
    import org.springframework.stereotype.Component;
     
    /**
     * 接口实现
     * 
     * @author leftso
     *
     */
    @WebService(serviceName = "CommonService", // 与接口中指定的name一致
            targetNamespace = "http://webservice.leftso.com/", // 与接口中的命名空间一致,一般是接口的包名倒
            endpointInterface = "com.leftso.webservice.CommonService"// 接口地址
    )
    @Component
    public class CommonServiceImp implements CommonService {
     
        @Override
        public String sayHello(String name) {
     
            return "Hello ," + name;
        }
     
    }
    复制代码

    4.配置cxf服务发布,默认服务在Host:port/services/***路径下

    复制代码
    package com.leftso.config;
     
    import javax.xml.ws.Endpoint;
     
    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 com.leftso.webservice.CommonService;
     
    @Configuration
    public class CxfConfig {
        @Autowired
        private Bus bus;
     
        @Autowired
        CommonService commonService;
     
        /** JAX-WS **/
        @Bean
        public Endpoint endpoint() {
            EndpointImpl endpoint = new EndpointImpl(bus, commonService);
            endpoint.publish("/CommonService");
            return endpoint;
        }
    }
    复制代码

    这里相当于把Commonservice接口发布在了路径/services/CommonService下,wsdl文档路径为
    http://localhost:8080/services/CommonService?wsdl

    创建基于cxf的客服端调用webservice接口(非使用wsdl文档生成java类)

    复制代码
    package com.leftso.client;
     
    import org.apache.cxf.endpoint.Client;
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
    import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
     
    import com.leftso.webservice.CommonService;
     
    public class CxfClient {
        public static void main(String[] args) {
            cl1();
        }
     
        /**
         * 方式1.代理类工厂的方式,需要拿到对方的接口
         */
        public static void cl1() {
            try {
                // 接口地址
                String address = "http://localhost:8080/services/CommonService?wsdl";
                // 代理工厂
                JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
                // 设置代理地址
                jaxWsProxyFactoryBean.setAddress(address);
                // 设置接口类型
                jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
                // 创建一个代理接口实现
                CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
                // 数据准备
                String userName = "Leftso";
                // 调用代理接口的方法调用并返回结果
                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:8080/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", "Leftso");
                System.out.println("返回数据:" + objects[0]);
            } catch (java.lang.Exception e) {
                e.printStackTrace();
            }
        }
    }
    复制代码
    调用后返回结果输出为
    Hello,Leftso

     

  • 相关阅读:
    第一次离职了
    华为手机 关于Android真机调试的时候logcat中无法输出调试信息的解决办法
    ListView的简单实用
    博客生涯开始
    hdu1151:Air Raid(最小路径覆盖)
    hdu1151:Air Raid(最小路径覆盖)
    hdu1162:Eddy's picture(最小生成树)
    hdu1162:Eddy's picture(最小生成树)
    hdu1301:Jungle Roads(最小生成树)
    hdu1301:Jungle Roads(最小生成树)
  • 原文地址:https://www.cnblogs.com/wq-9/p/12510777.html
Copyright © 2011-2022 走看看