根据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:\temp下,生成包结构为cn.ljl.sand.jws.chapter3.client.wsimport的java源文件的命令为:
wsimport -s F:\temp -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:\temp下,生成包结构为cn.ljl.sand.jws.chapter3.client.wsimport的interpret-wsimport.jar的命令为:

wsimport -d F:\temp -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,可以根据需要来自配置。
 

 

posted @   整合侠  阅读(1031)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示