zoukankan      html  css  js  c++  java
  • 根据wsdl,基于wsimport生成代码的客户端

    根据wsdl,基于wsimport生成代码的客户端

    wsimport是jdk自带的命令,可以根据wsdl文档生成客户端中间代码,基于生成的代码编写客户端,可以省很多麻烦。

    局限性:wsimport  是根据JDK1.6.0_21及以上的生成本地代码的,它只能解析服务器端的SOAP协议为1.1,不能解析SOAP1.2的协议。如果解析SOAP1.2 将会解析不完全。

    wsimport的用法:

    wsimport [options] <WSDL_URI>

     

    比较常用的[options]有:
    1. -d <directory>
       在指定的目录生成class文件
    2. -clientjar <jarfile>
       在当前目录生成jar文件,结合-d <directory>可以在指定的目录生成jar文件
    3. -s <directory>
       在指定的目录生成java源文件
    4. -p <pkg>
       指定生成文件的包结构
    5. -keep
       在生成class文件,或者jar包时,同时保留java源文件
     
    常用的组合:
    1,在指定的目录生成指定包结构的java源文件
    假设wsdl文档的uri为http://localhost:6666/service/interpret?wsdl,那么在F: emp下,生成包结构为cn.ljl.sand.jws.chapter3.client.wsimport的java源文件的命令为:
    wsimport -s F:	emp -p cn.ljl.sand.jws.chapter3.client.wsimport http://localhost:6666/service/interpret?wsdl

    2,在指定的目录生成指定包结构的jar文件
    假设wsdl文档的uri为http://localhost:6666/service/interpret?wsdl,那么在F: emp下,生成包结构为cn.ljl.sand.jws.chapter3.client.wsimport的interpret-wsimport.jar的命令为:

    wsimport -d F:	emp -clientjar interpret-wsimport.jar -p cn.ljl.sand.jws.chapter3.client.wsimport http://localhost:6666/service/interpret?wsdl

    核心类介绍:

    wsimport生成的文件中,有两个是我们需要了解的,一个是以wsdl文档的portType元素的name为名的接口,一个是以wsdl文档的service元素的name为名的类。

    编写客户端代码(调用生成的客户端代码):

    创建WSIClient.java,基于生成的代码访问服务。

    
    
    package cn.ljl.sand.jws.chapter3.client;
    import java.net.MalformedURLException;
    import java.net.URL;
    import org.junit.Assert;
    import org.junit.Test;
    import cn.ljl.sand.jws.chapter3.client.wsimport.InterpretService;
    import cn.ljl.sand.jws.chapter3.client.wsimport.InterpretServiceImplService;
    public class WSIClient {
        @Test
        public void test() {
            InterpretServiceImplService ss = new InterpretServiceImplService();
            InterpretService service = ss.getInterpretServiceImplPort();
            String chnum = service.interpret(112358);
            Assert.assertEquals(">>>>>>>>>:", chnum);
        }
        
        @Test
        public void test2() throws MalformedURLException {
            URL url = new URL("http://localhost:6666/service/interpret?wsdl");
            InterpretServiceImplService ss = new InterpretServiceImplService(url);
            InterpretService service = ss.getInterpretServiceImplPort();
            String chnum = service.interpret(112358);
            Assert.assertEquals(">>>>>>>>>>:", chnum);
        }
    }
    
    
    说明:提供了两个测试方法:test使用最简单的方式;test2考虑到URL可能的变动,所以单独指定了URL,而这个url,可以根据需要来自配置。
     

     

  • 相关阅读:
    vue2.x 时间范围 date range timepicker。只在项目中使用elementUI的date-picker
    FE 命令随笔
    [one day one question] 有没有免费接收短信验证用于注册的软件或者平台?
    [one day one question] webpack 打包报错 Cannot assign to read only property 'exports' of object '#<Object>'
    [one day one question] GIF动画为什么只动一次不能循环
    [one day one question] Vue单页面应用如何保证F5强刷不清空数据
    [one day one question] express 不缓存如何实现
    [one day one question] nodejs require 缓存,无法检测文件变化
    [one day one question] Iscroll 5.0 在chrome上无法滑动
    [one day one question] Vue数组变更不能触发刷新
  • 原文地址:https://www.cnblogs.com/lizm166/p/7997907.html
Copyright © 2011-2022 走看看