zoukankan      html  css  js  c++  java
  • webservice 动态调用使用技巧

    参考使用

    • maven 引用
     <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>
    • 代码
    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    Client client = dcf.createClient("echo.wsdl");
     
    Object[] res = client.invoke("echo", "test echo");
    System.out.println("Echo response: " + res[0]);

    常见问题

    • org.apache.cxf.interceptor.Fault: Unmarshalling Error: unexpected element
      忽略错误即可 但是需要注意实际是否需要进行数据校验(以下代码同时添加了一个client 的cache 处理)
     
    public class WsClient {
     
        private  static  final  Logger logger = LoggerFactory.getLogger(WsClient.class);
        private  static final Cache<String, Client> cache = Caffeine.newBuilder()
                .expireAfterAccess(10, TimeUnit.MINUTES)
                .maximumSize(10_000)
                .build();
     
        public static  Client createClient(String endpoint) {
            logger.info("create ws client:{}",endpoint);
            Client client = JaxWsDynamicClientFactory.newInstance().createClient(endpoint + "?wsdl");
            // ignore validation-event-handler fix https://access.redhat.com/solutions/2047853
            client.getRequestContext().put("set-jaxb-validation-event-handler", false);
            return client;
        }
        public static Client getClient(String endpoint) {
           return cache.get(endpoint,wsURL->createClient(wsURL));
        }
    }

    说明

    JaxWsDynamicClientFactory 内部是比较复杂了,使用了动态代码编译以及类加载机制,可以简化我们很多ws 调用的处理

    参考资料

    https://cxf.apache.org/docs/dynamic-clients.html

  • 相关阅读:
    android shape的使用(转)
    使用BigDecimal进行精确运算以及格式化输出数字
    MyTextBoxControls正式推出第一个版本T1.1.0.1
    排列组合与回溯算法
    弹出一个层屏蔽页面登录
    一级MS软件下载
    c#:文件对话框(FileDialog)
    采用正则表达式获取图片地址
    超级实用且不花哨的js代码大全
    常用到的一些正则表达式
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/15704251.html
Copyright © 2011-2022 走看看