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

简介

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

  • 是jdk自带的工具

  • 位置:C:Program FilesJavajdk1.8.0_181in

使用方法

打开cmd

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

D:idea_workspacexxxxxx-apisrcmainjava>

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
原文地址:https://www.cnblogs.com/stonechen/p/14378083.html