This post will describe its how easy to create web service client using cxf.
http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL
save it at src/main/wsdl/weather.wsdl
open the pom.xml
create a tag <build></build> inside <project> tag if not existing.
create a <plugins></plugins> inside <build> tag
paste following within <plugins> tags
use following command to generate the java classes from wsdl.
mvn generate-sources
create a jar file using
mv install
java codes should be created at target folder.
new java files should be able to located at
target/generated/cxf
to use the code in our code, for eclipse we will have to add the newly created jar to class path.
now go to main java class.
and update it to call the web service.
out put will be
NOTE:
eclipse validation will requests to put plugins tag inside plugins management in the pom.xml. Even though this will solve eclipse issue, it will not create the java files using wsdl. so make sure plugins tag is directly under build tag.
create simple maven project
- open eclipse
- create maven project quick start
WSDL
I will using weather service to do the deployment. get the wsdl fromhttp://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL
save it at src/main/wsdl/weather.wsdl
add cxf plugin
open the pom.xml
create a tag <build></build> inside <project> tag if not existing.
create a <plugins></plugins> inside <build> tag
paste following within <plugins> tags
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>target/generated/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>src/main/wsdl/weather.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>target/generated/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>src/main/wsdl/weather.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
implementation
generate-sourcesuse following command to generate the java classes from wsdl.
mvn generate-sources
create a jar file using
mv install
java codes should be created at target folder.
new java files should be able to located at
target/generated/cxf
to use the code in our code, for eclipse we will have to add the newly created jar to class path.
now go to main java class.
and update it to call the web service.
public static void main( String[] args )
{
System.out.println( "Hello World!" );
Weather weather = new Weather();
WeatherSoap weatherSoap = weather.getWeatherSoap();
ForecastReturn forecastReturn=weatherSoap.getCityForecastByZIP("94025");
List< Forecast > forecasts = forecastReturn.getForecastResult().getForecast();
for (Forecast forecast : forecasts ){
System.out.println("------------------------------");
System.out.println(forecast.getDate().toString());
System.out.println(forecast.getProbabilityOfPrecipiation().getDaytime().toString() );
System.out.println(forecast.getTemperatures().getDaytimeHigh().toString());
System.out.println(forecast.getDesciption().toString());
System.out.println(forecast.getWeatherID() );
System.out.println(forecast.getClass().toString());
}
}
{
System.out.println( "Hello World!" );
Weather weather = new Weather();
WeatherSoap weatherSoap = weather.getWeatherSoap();
ForecastReturn forecastReturn=weatherSoap.getCityForecastByZIP("94025");
List< Forecast > forecasts = forecastReturn.getForecastResult().getForecast();
for (Forecast forecast : forecasts ){
System.out.println("------------------------------");
System.out.println(forecast.getDate().toString());
System.out.println(forecast.getProbabilityOfPrecipiation().getDaytime().toString() );
System.out.println(forecast.getTemperatures().getDaytimeHigh().toString());
System.out.println(forecast.getDesciption().toString());
System.out.println(forecast.getWeatherID() );
System.out.println(forecast.getClass().toString());
}
}
out put will be
Hello World!
------------------------------
2013-01-03T00:00:00
10
56
Partly Cloudy
2
class com.cdyne.ws.weatherws.Forecast
------------------------------
2013-01-04T00:00:00
00
57
Partly Cloudy
2
class com.cdyne.ws.weatherws.Forecast
------------------------------
2013-01-05T00:00:00
10
58
Partly Cloudy
2
class com.cdyne.ws.weatherws.Forecast
------------------------------
2013-01-06T00:00:00
20
56
Partly Cloudy
2
class com.cdyne.ws.weatherws.Forecast
------------------------------
2013-01-07T00:00:00
10
58
Partly Cloudy
2
class com.cdyne.ws.weatherws.Forecast
------------------------------
2013-01-08T00:00:00
10
58
Partly Cloudy
2
class com.cdyne.ws.weatherws.Forecast
------------------------------
2013-01-09T00:00:00
10
57
Partly Cloudy
2
class com.cdyne.ws.weatherws.Forecast
------------------------------
2013-01-03T00:00:00
10
56
Partly Cloudy
2
class com.cdyne.ws.weatherws.Forecast
------------------------------
2013-01-04T00:00:00
00
57
Partly Cloudy
2
class com.cdyne.ws.weatherws.Forecast
------------------------------
2013-01-05T00:00:00
10
58
Partly Cloudy
2
class com.cdyne.ws.weatherws.Forecast
------------------------------
2013-01-06T00:00:00
20
56
Partly Cloudy
2
class com.cdyne.ws.weatherws.Forecast
------------------------------
2013-01-07T00:00:00
10
58
Partly Cloudy
2
class com.cdyne.ws.weatherws.Forecast
------------------------------
2013-01-08T00:00:00
10
58
Partly Cloudy
2
class com.cdyne.ws.weatherws.Forecast
------------------------------
2013-01-09T00:00:00
10
57
Partly Cloudy
2
class com.cdyne.ws.weatherws.Forecast
NOTE:
eclipse validation will requests to put plugins tag inside plugins management in the pom.xml. Even though this will solve eclipse issue, it will not create the java files using wsdl. so make sure plugins tag is directly under build tag.
Comments
Post a Comment