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,可以根据需要来自配置。
     

     

  • 相关阅读:
    bestcoder#45 1002 求区间的逆序数 树状数组
    poj2299 Ultra-QuickSort 树状数组求逆序数
    codeforces#323(div2) C. GCD Table 贪心
    bestcoder#58(div2) 1002 LCS 置换
    codeforces#321(div2) E. Kefa and Watch
    1sting
    HDU1013 Digital Roots
    【编程思想】【设计模式】【行为模式Behavioral】catalog
    【编程思想】【设计模式】【行为模式Behavioral】chain
    【编程思想】【设计模式】【结构模式Structural】代理模式Proxy
  • 原文地址:https://www.cnblogs.com/lizm166/p/7997907.html
Copyright © 2011-2022 走看看