close

This is an ongoing blog to Web Services development. In my previous blog, I talked about JAX-WS framework in detail and how to create code first and contract web services. In this blog, I would describe how to invoke .NET web services using JAX-WS.

The promise of web services lies is the interoperability between services that are running on different platforms and implemented using different programming languages. A web service created in a .NET platform can be invoked by any client application implemented in different programming language like Java. As web services are defined in terms of service interfaces based on standards like XML, SOAP and WSDL, implementation of web services using Java or .NET is matter of realization of the specification.

With the web service specifications laid down, there was frequent interoperability issues, like a .NET client wasn’t able invoke a Java web service, as it used some RPC encoding mechanism which was interpreted differently by .NET client. In order to make sure that vendors providing web services implementation interoperate with each other, the Web Services Interoperability Organization (WS-I), an open industry organization was formed to promote Web services interoperability across platforms, operating systems, and programming languages. One of the concrete work products of WS-I is the Basic Profile. A basic profile narrows the scope of specifications to a reasonable set of rules and guidelines that are best suited to help interoperability. JAX-WS supports WS-I standard and the WSDL artifacts generated from code-first approach adheres to WS-I Basic Profile 1.1 standard. The WS-I Basic profile disallows the use of any encoding mechanism as they might be interpreted differently by different vendors. Adhering to WSI Profiles ensures that your services can interoperate within any platform.

Invoking NET Services

The process of generating web service clients doesn’t differ for .NET or Java web services, as you generated web service clients from WSDL and XML Schema definitions. You will take a publicly available .NET WS-I Compatible service located at http://www.ignyte.com/webservices/ignyte.whatsshowing.webservice/moviefunctions.asmx?wsdl . This web service retreives US Theaters and Movie Showtimes information based on the a valid US zip code and a radius supplied by the web service clients.

Creating Web Service Client

You will start off by generating the JAX-WS artifacts from the Theatre and Movie Showtimes WSDL using the wsimport tool. You will then create a web service client, which uses artifact code generated to invoke the Theatre and Movie Showtimes web service.

For generating the JAX-WS artifacts , navigate to navigate to bin directory which contains all the compiled  class files (previously created as a result of executing compile.bat) [JF1] and run the following command.

wsimport -keep -p com.nb.beginjava6.netservice.client http://www.ignyte.com/webservices/ignyte.whatsshowing.webservice/moviefunctions.asmx?wsdl

The following artifacts get generated from the WSDL

  • JAXB Classes (ArrayOfMovie, ArrayOfTheater, ArrayOfUpcomingMovie, Movie, Theater and UpcomingMovie):  These are generated by reading the schema definitions defined in Theatre and Movie Showtimes WSDL.
  • The RequestWrapper and ResponseWrapper classes: The Wrapper classes were discussed earlier. As there are two operations defined in WSDL, 4 wrapper classes are generated. The GetTheatersAndMovies (Request Wrapper) and GetTheatersAndMoviesResponse (Response Wrapper) for getTheatersAndMovies operation and GetUpcomingMovies and GetUpcomingMoviesResponse for getUpcomingMovies operation.
  • Service class (MovieInformation): This is the class that your clients will use to make requests to the web service.
  • Service Interface (MovieInformationSoap): This class contains the service interface for the Theatre and Movie Showtimes web service.

As discussed earlier in the web service client creation section, you will use the above generated artifact for creating the web service client. A sample reference code is provided in com.nb.beginjava6.netservice.client package. The code for web service client is provided below.


package com.nb.beginjava6.netservice.client;

import java.util.List;

public class MovieWebServiceClient {

public static void main(String[] args) {

if (args.length < 1) {

System.out.println("Specify Valid Zip Code and Zip Code Range");

System.exit(-1);

}

String zipCode = args[0];

int radius = 0;

if (args.length == 2) {

radius = Integer.parseInt(args[1]);

}

MovieWebServiceClient client = new MovieWebServiceClient();

client.getMovieInformation(zipCode, radius);

}

private void getMovieInformation(String zipCode, int radius) {

MovieInformation movieService = new MovieInformation();

MovieInformationSoap port =

movieService.getMovieInformationSoap();

ArrayOfTheater theaterInfo = port.getTheatersAndMovies(zipCode,

radius);

System.out.println("Theater List is : "

+ theaterInfo.getTheater().size());

List<Theater> theatreList = theaterInfo.getTheater();

for (int i = 0; i < theatreList.size(); i++) {

System.out.println("Theatre Name : " +

theatreList.get(i).getName());

List<Movie> movieList =

theatreList.get(i).getMovies().getMovie();

for (int j = 0; j < movieList.size(); j++) {

System.out.println("Movie Name : " +

movieList.get(j).getName());

}

System.out.println("End of Movies for Theatre :"

+ theatreList.get(i).getName());

}

}

}

Code: The code listing for web service client calling a .NET service.

The web service client code listed above does the following:

  • Creates an instance of service class (MovieInformation).
  • Retrieves a proxy to the service, also known as a port, by invoking getMovieInformationSoap on the service. The port implements the service interface defined by the service.
  • Invokes the port’s getTheatersAndMovies method, passing in a valid US zip code and radius.
  • Gets the reponse i.e ArrayOfTheater from the service.
  • Get the List of theatres using the getTheater() method on ArrayOfTheater object.
  • For each theatre , prints the theatre name and gets movie listing object (ArrayOfMovie) for that theatre using the getMovies() method on the theatre object.
  • Get the List of Movies from ArrayOfTheater object and prints the Movie name at the console.

Execute the web service client by providing the valid US Zip code (like 78750) and range (say 100) using the following command.


java com.nb.beginjava6.netservice.client.MovieWebServiceClient 78750 100

At the console you will receive the following output. The following shows movie listing for one Theatre . Similarly the information will repeat for all theatres.


Theater List is : 21

Theatre Name : Alamo Drafthouse Lake Creek

Movie Name : Dj Vu

Movie Name : Rocky Balboa

Movie Name : Stranger Than Fiction

Movie Name : The Departed

Movie Name : We Are Marshall

End of Movies for Theatre :Alamo Drafthouse Lake Creek

You have now successfully invoked a .NET service using a JAX-WS client.

Hope the web service development blogs have given you a speed start to developing web services.


Tags : .NET JAVAweb service tutorialwebservices
Navveen

The author Navveen