Generate a WebService client code instance using the wsimport command

in the bin folder of the JDK , There is a wsimport.exe tool that can generate corresponding class files based on wsdl files, copy these class files that exist locally to the project that needs to be used, and then call the methods provided by webService just like calling local classes. This tool can be used for non-Java servers, such as WebService written in C#, and generate Java client implementations through wsimport.

Common commands are as follows :

This command must run cmd as an administrator under windows Command window, and then execute this command.

[html] view plain copy

  1. wsimport-keep-d D:\temp\d-s D:\temp\s-p com.map-verbose http: //ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

-keep: whether to generate java Source file

-d: Specify the output directory of the .class file

-s: Specify the output directory of the .java file

-p:defines the package name of the generated class, not If defined, there is a default package name

-verbose: display output information on the console

-b: Specify jaxws/jaxb binding files or additional schemes p>

-extension: Use extension to support SOAP1.2

wsimport example

Here, recommend a website,provides a comprehensive WebService interface for Reference: http://www.webxml.com.cn/zh_cn/web_services .aspx

Generate the webService of the place where the query number belongs locally h3>

(1 ) Enter the website to find

Among them, the .asmx URL describes the calling method, input parameters, and return data,.wsdl is the URL we want to use to generate the code.

(2) Create a new Java Project. After the project is created, generate the src directory

(3) cmd command, enter the bin directory of jdk and run the command:

[html] view plain copy

  1. wsimport-keep-s D:\Workspaces -p com.cn.phone -verbose http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

The path after -s if It is not the src directory of the project, you need to copy the generated package to the src directory


Refresh the project and you will see the files we generated

When we click on a few java files, do we find Chinese comments are garbled. If you want to change the encoding of the source code to UTF-8, you can download a tool: UTFCastExpress

< span style="margin:0px; padding:0px; font-family:"Microsoft YaHei"; font-size:14px">http://download.csdn.net/detail/aqsunkai/9535166

(4) Write the test class under this path:

[jav a] view plain copy

< /span>

    < li class="alt" style="margin-left:40px; list-style:decimal; border-top:none; border-right:none; border-bottom:none; border-left:3px solid rgb(108,226,108); color:inherit; line-height:18px; margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; padding:0px 3px 0px 10px!important"> packagecom.cn.phone;
  1. publicclassPhoneWsClient{
  2. publicstaticvoidmain(String[]args){
  3. //Create a MobileCodeWS factory
  4. MobileCodeWS factory = newMobileCodeWS();
  5. //root According to the factory, create a MobileCodeWSSoap object
  6. MobileCodeWSSoap mobileCodeWSSoap=factory.getMobileCodeWSSoap();
  7. //call The getMobileCodeInfo method provided by WebService to query the attribution of the mobile phone number
  8. String searchResult= mobileCodeWSSoap.getMobileCodeInfo(“18512155752”, null);
  9. System.out.println(searchResult);
  10. }< /li>

Run main After the method:


in Local generation query weather forecastwebService

When using wsimport directly to generate a Java file through this address, an error will be reported. Because the wsdl contains references such as ref = “s:schema”. And jaxb is not supported. So you need to manually download the wsdl to make the next modification, and then generate the java file.
(1) Save the page as
< /span>

(2) All
< /span> Change to , there are three places that need to be modified , It is recommended to find< / span>, when modifyingalso delete
< img alt="" src="/wp-content/uploadshttp:/img.voidcn.com/vcimg/static/loading.png" style="border:0px; vertical-align:middle; outline:none; max-width :100%" d="14152092" s="b7c_d0a" t="jpg">
(3) Run command:

[java] view plain copy

  1. wsimport -keep -s D:\Workspaces -p com.cn.weather -verbose C:\Users\sun\Desktop\WeatherWS.xml  

(4) 把生成的包拷贝到src目录下
需要注意的是,要修改WeatherWS.java文件中wsdl的url,因为我们用的是本地文件生成的,要修改成网站的url:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
(5)  在该路径下写测试类
[java]  view plain  copy

  1. package com.cn.weather;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class WeatherWsClient {  
  6.   
  7.     public static void main(String[] args) {  
  8.          //创建一个WeatherWS工厂  
  9.          WeatherWS factory = new WeatherWS();  
  10.          //根据工厂创建一个WeatherWSSoap对象  
  11.          WeatherWSSoap weatherWSSoap = factory.getWeatherWSSoap();  
  12.          //调用WebService提供的getWeather方法获取南宁市的天气预报情况  
  13.          ArrayOfString weatherInfo = weatherWSSoap.getWeather(“钓鱼岛”null);  
  14.          List lstWeatherInfo = weatherInfo.getString();  
  15.          //遍历天气预报信息  
  16.          for (String string : lstWeatherInfo) {  
  17.            System.out.println(string) ;  
  18.            System.out.println(“————————“);  
  19.           }  
  20.          //获得中国省份、直辖市、地区和与之对应的ID  
  21.          ArrayOfString s = weatherWSSoap.getRegionProvince();  
  22.          List list = s.getString();  
  23.          for (String string : list) {  
  24.                System.out.println(string);  
  25.                System.out.println(“————————“);  
  26.          }  
  27.     }  
  28. }  
运行main方法后:

[html]  view plain  copy

  1. wsimport -keep -d D:\temp\d -s D:\temp\s -p com.map -verbose http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl  

[html]  view plain  copy

[html]  view plain  copy

[html]  view plain  copy

  1. wsimport -keep -s D:\Workspaces -p com.cn.phone -verbose http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl  

[html]  view plain  copy

[html]  view plain  copy

[java]  view plain  copy

  1. package com.cn.phone;  
  2.   
  3. public class PhoneWsClient {  
  4.   
  5.     public static void main(String[] args){  
  6.         //创建一个MobileCodeWS工厂  
  7.         MobileCodeWS factory = new MobileCodeWS();  
  8.         //根据工厂创建一个MobileCodeWSSoap对象  
  9.         MobileCodeWSSoap mobileCodeWSSoap = factory.getMobileCodeWSSoap();  
  10.         //调用WebService提供的getMobileCodeInfo方法查询手机号码的归属地  
  11.         String searchResult = mobileCodeWSSoap.getMobileCodeInfo(“18512155752”null);  
  12.         System.out.println(searchResult);  
  13.   
  14.     }  
  15. }  

[java]  view plain  copy

[java]  view plain  copy

直接使用wsimport 通过该地址生成Java文件时,会报错。因为该wsdl里面包含 ref = “s:schema” 这样的引用。而jaxb是不支持的。所以需要手动将该wsdl下载下来做下修改,然后再生成java文件。

(1) 将页面另存为


(2) 将所有的
                 
成 ,一共有三处需要修改,建议查找,修改时把也要删掉


(3) 运行命令:

[java]  view plain  copy

  1. wsimport -keep -s D:\Workspaces -p com.cn.weather -verbose C:\Users\sun\Desktop\WeatherWS.xml  

[java]  view plain  copy

[java]  view plain  copy


(4) 把生成的包拷贝到src目录下

需要注意的是,要修改WeatherWS.java文件中wsdl的url,因为我们用的是本地文件生成的,要修改成网站的url:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl

(5)  在该路径下写测试类

[java]  view plain  copy

  1. package com.cn.weather;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class WeatherWsClient {  
  6.   
  7.     public static void main(String[] args) {  
  8.          //创建一个WeatherWS工厂  
  9.          WeatherWS factory = new WeatherWS();  
  10.          //根据工厂创建一个WeatherWSSoap对象  
  11.          WeatherWSSoap weatherWSSoap = factory.getWeatherWSSoap();  
  12.          //调用WebService提供的getWeather方法获取南宁市的天气预报情况  
  13.          ArrayOfString weatherInfo = weatherWSSoap.getWeather(“钓鱼岛”null);  
  14.          List lstWeatherInfo = weatherInfo.getString();  
  15.          //遍历天气预报信息  
  16.          for (String string : lstWeatherInfo) {  
  17.            System.out.println(string);  
  18.            System.out.println(“————————“);  
  19.           }  
  20.          //获得中国省份、直辖市、地区和与之对应的ID  
  21.          ArrayOfString s = weatherWSSoap.getRegionProvince();  
  22.          List list = s.getString();  
  23.          for (String string : list) {  
  24.                System.out.println(string);  
  25.                System.out.println(“————————“);  
  26.          }  
  27.     }  
  28. }  

[java]  view plain  copy

[java]  view plain  copy

运行main方法后:

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3525 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.