使用wsimport对接WebService 生成Java客户端

简介

  • 用于生成非java开发webservice的客户端程序

  • 是jdk自带的工具

  • 位置:C:\Program Files\Java\jdk1.8.0_181\bin

使用方法

打开cmd

1. 切换到项目的java目录下

D:\idea_workspace\xxxxxx-api\src\main\java>

2. 执行命令

> wsimport -p com.xxx.wsclient.unitbasic -keep http://xxx.com/UnitWebservice/ws/unitbasic?wsdl
  • -p [package] 自定义生成的包名

  • -keep [wsdl] 生成Java源文件

其他参数

-keep:是否生成java源文件

-d:指定.class文件的输出目录

-s:指定.java文件的输出目录

-p:定义生成类的包名,不定义的话有默认包名

-verbose:在控制台显示输出信息

-b:指定jaxws/jaxb绑定文件或额外的schemas

-extension:使用扩展来支持SOAP1.2

-encoding :设置编码格式

wsimport -encoding utf-8 -p com.xxx.wsclient.unitbasic -keep http://xxx.com/UnitWebservice/ws/unitbasic?wsdl

帮助命令

wsimport -help

Java实例

代码片段

// 请求接口 UnitBasicInfoServiceService unitBasicInfoServiceService = new UnitBasicInfoServiceService(); UnitBasicInfoManager unitBasicInfoServicePort = unitBasicInfoServiceService.getUnitBasicInfoServicePort(); // XML使用的视图对象 UnitInfoVo unitInfoVo = unitBasicInfoServicePort.unitInfo(wsclient.getKeyCode(), unitCode);

Maven依赖

<!-- .net webservice client --> <!-- https://mvnrepository.com/artifact/com.sun.xml.fastinfoset/FastInfoset --> <dependency> <groupId>com.sun.xml.fastinfoset</groupId> <artifactId>FastInfoset</artifactId> <version>1.2.12</version> </dependency> <!-- https://mvnrepository.com/artifact/org.glassfish.gmbal/gmbal-api-only --> <dependency> <groupId>org.glassfish.gmbal</groupId> <artifactId>gmbal-api-only</artifactId> <version>3.1.0-b001</version> </dependency> <!-- https://mvnrepository.com/artifact/org.glassfish.ha/ha-api --> <dependency> <groupId>org.glassfish.ha</groupId> <artifactId>ha-api</artifactId> <version>3.1.8</version> </dependency> <!-- https://mvnrepository.com/artifact/org.glassfish/javax.annotation --> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.annotation</artifactId> <version>3.1.1-b06</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api --> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.2.7</version> </dependency> <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl --> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.2.7</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api --> <!-- <dependency>--> <!-- <groupId>javax.xml.bind</groupId>--> <!-- <artifactId>jaxb-api</artifactId>--> <!-- <version>2.2.6</version>--> <!-- </dependency>--> <!-- https://mvnrepository.com/artifact/javax.xml.ws/jaxws-api --> <dependency> <groupId>javax.xml.ws</groupId> <artifactId>jaxws-api</artifactId> <version>2.2.8</version> </dependency> <!-- https://mvnrepository.com/artifact/com.sun.xml.ws/jaxws-rt --> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version>2.2.7</version> </dependency> <!-- https://mvnrepository.com/artifact/com.sun.xml.ws/jaxws-tools --> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-tools</artifactId> <version>2.2.7</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.jws/jsr181-api --> <dependency> <groupId>javax.jws</groupId> <artifactId>jsr181-api</artifactId> <version>1.0-MR1</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.mail/mail --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.glassfish.external/management-api --> <dependency> <groupId>org.glassfish.external</groupId> <artifactId>management-api</artifactId> <version>3.0.0-b012</version> </dependency> <!-- https://mvnrepository.com/artifact/org.jvnet.mimepull/mimepull --> <dependency> <groupId>org.jvnet.mimepull</groupId> <artifactId>mimepull</artifactId> <version>1.8</version> </dependency> <!-- https://mvnrepository.com/artifact/com.sun.xml.ws/policy --> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>policy</artifactId> <version>2.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.xml.soap/saaj-api --> <dependency> <groupId>javax.xml.soap</groupId> <artifactId>saaj-api</artifactId> <version>1.3.4</version> </dependency> <!-- https://mvnrepository.com/artifact/com.sun.xml.messaging.saaj/saaj-impl --> <dependency> <groupId>com.sun.xml.messaging.saaj</groupId> <artifactId>saaj-impl</artifactId> <version>1.3.19</version> </dependency> <!-- https://mvnrepository.com/artifact/org.codehaus.woodstox/stax2-api --> <dependency> <groupId>org.codehaus.woodstox</groupId> <artifactId>stax2-api</artifactId> <version>3.1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.jvnet.staxex/stax-ex --> <dependency> <groupId>org.jvnet.staxex</groupId> <artifactId>stax-ex</artifactId> <version>1.7</version> </dependency> <!-- https://mvnrepository.com/artifact/com.sun.xml.stream.buffer/streambuffer --> <dependency> <groupId>com.sun.xml.stream.buffer</groupId> <artifactId>streambuffer</artifactId> <version>1.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.codehaus.woodstox/woodstox-core-asl --> <dependency> <groupId>org.codehaus.woodstox</groupId> <artifactId>woodstox-core-asl</artifactId> <version>4.1.2</version> </dependency>

使用中存在的问题

1、不使用wsimport自动生成的class文件,要手动删除

解决方案:使用-d 设置class文件输出目录,输出目录如果不存在,需要提前创建。

2、wsimport自动生成的webservice源代码中注释是GBK编码,在UTF-8下会出现中文乱码

解决方案:

-encoding:设置编码格式

wsimport -encoding utf-8 -p com.xxx.wsclient.unitbasic -keep http://xxx.com/UnitWebservice/ws/unitbasic?wsdl

__EOF__

本文作者stonechen
本文链接https://www.cnblogs.com/stonechen/p/14378083.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   没事摸摸小肚子  阅读(518)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示