使用idea和wsdl生成java代碼

  1. 建立一個簡單的java helloworld項目java

選擇next:web

選擇java helloworld project next框架

填寫project name finish測試

2.  建立好項目後 選擇項目 右鍵->webservice->Generate code

生成代碼配置,填寫wsdl路徑,包路徑,webservice平臺(這裏我選擇的是jax-ws jdk自帶的webservice實現方式,不用再下載jar包)xml

這裏我使用的wsdl,是一個天氣預報的webservice:http://www.webservicex.com/globalweather.asmx?WSDL get

選擇finish 等待片刻就可看見生成的代碼了:webservice

3. 測試代碼it

在Main Class中填寫以下代碼 io

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、等

相關文章
相關標籤/搜索