使用idea和wsdl生成java代码

  1. 创建一个简单的java helloworld项目

    技术分享

选择next:

技术分享

选择java helloworld project next

技术分享

填写project name finish

2.  创建好项目后 选择项目 右键->webservice->Generate 

技术分享

生成代码配置,填写wsdl路径,包路径,webservice平台(这里我选择的是jax-ws jdk自带的webservice实现方式,不用再下载jar包)

技术分享

这里我使用的wsdl,是一个天气预报的webservice:http://www.webservicex.com/globalweather.asmx?WSDL

选择finish 等待片刻就可看见生成的代码了:

技术分享

3. 测试代码

在Main Class中填写如下代码 

import weather.GlobalWeather;
import weather.GlobalWeatherSoap;

public class Main {

    public static void main(String[] args) {
        GlobalWeather service = new GlobalWeather();
        GlobalWeatherSoap port = service.getGlobalWeatherSoap();
        String body = port.getWeather("shanghai", "China");
        System.out.println(body);
    }
}

运行结果如下:

<?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
  <Location>Shanghai / Hongqiao, China (ZSSS) 31-10N 121-26E 3M</Location>
  <Time>Jul 22, 2015 - 12:00 AM EDT / 2015.07.22 0400 UTC</Time>
  <Wind> Variable at 2 MPH (2 KT):0</Wind>
  <Visibility> greater than 7 mile(s):0</Visibility>
  <SkyConditions> mostly clear</SkyConditions>
  <Temperature> 84 F (29 C)</Temperature>
  <DewPoint> 78 F (26 C)</DewPoint>
  <RelativeHumidity> 83%</RelativeHumidity>
  <Pressure> 29.65 in. Hg (1004 hPa)</Pressure>
  <Status>Success</Status>
</CurrentWeather>

生成代码的平台框架,也可以选择axis 、cxf、等

文章来自:http://my.oschina.net/nba/blog/482117
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3