Posts
12
Comments
68
Trackbacks
0
.NET WCF services and Java Web Services Interoperability - Part 2 of 3

Part II - Create the WCF "Team" service consumed by a Java client

This article is the second in a three part series demonstrating the interoperability between .NET WCF services and Java Web Services.

NOTE: If you haven't read Part I you should review that article before continuing on.

In Part I we reviewed the class diagram of the Team class, so our first step is to create the .NET classes in support of that model. We will start by creating the Player class:

   1:  [DataContractAttribute(Name = "Player", Namespace = "HelloWorldWCFs")]
   2:  public class Player
   3:  {
   4:      [DataMemberAttribute(Name="Name",IsRequired=true)]
   5:      public string Name { get; set; }
   6:   
   7:      [DataMemberAttribute(Name = "Age", IsRequired = true)]
   8:      public int Age { get; set; }
   9:  }


With WCF, all classes must have a data contract established so that they can be serialized to XML and sent down the wire. Here we created a simple Player class decorated with the DataContractAttribute. This class contains Name and Age properties for each player and are decorated them with DataMemberAttribute attribute.

Next we will create the TeamType enumeration:

 

   1:  [DataContractAttribute(Name = "TeamType", Namespace = "HelloWorldWCFs")]
   2:  public enum TeamType : int
   3:  {
   4:      [EnumMemberAttribute(Value="Soccer")]
   5:      Soccer = 0,
   6:      [EnumMemberAttribute(Value = "Football")]
   7:      Football = 1
   8:  }


With this enumeration, we simply want the user to be able to either choose Soccer or Football as an option for a team type.

The next step is to create the Team class:

 

   1:  [DataContractAttribute(Name = "Team", Namespace = "HelloWorldWCFs")]
   2:  [XmlInclude(typeof(TeamType))]
   3:  [KnownType(typeof(TeamType))]
   4:  [XmlInclude(typeof(Player))]
   5:  [KnownType(typeof(Player))]
   6:  public class Team
   7:  {
   8:      [DataMemberAttribute(Name = "Name", IsRequired = true)]
   9:      public string Name { get; set; }
  10:   
  11:      [DataMemberAttribute(Name = "teamtype", IsRequired = true)]
  12:      public TeamType teamtype { get; set; }
  13:   
  14:      [DataMemberAttribute(Name = "Players", IsRequired = true)]
  15:      public List<Player> Players { get; set; }
  16:  }


The XmlInclude and KnownType decorations force the Player and TeamType classes to be included as part of the Team class in the serialization process.

Now the next step is to create the interface for our service:

 

   1:  [ServiceContract(Name="IHelloWorldWCF", Namespace = "HelloWorldWCFs")]
   2:  public interface IHelloWorldWCF
   3:  {
   4:      [OperationContract]
   5:      List<Team> getTeams();
   6:  }


As we can see, our service interface has one method getTeams() that will return a generic List of Team objects.

Finally we need to create the class that implements our new ServiceContract interface we just created:

 

   1:  public class HelloWorldWCF : IHelloWorldWCF
   2:  {    
   3:      public List<Team> getTeams()
   4:      {
   5:          return GetTeamInfo();
   6:      }
   7:   
   8:      private List<Team> GetTeamInfo()
   9:      {
  10:          var Memori = new Player() { Name = "Memori", Age = 15 };
  11:          var Mikayli = new Player() { Name = "Mikayli", Age = 12 };
  12:          var Myranda = new Player() { Name = "Myranda", Age = 11 };
  13:          var Elias = new Player() { Name = "Elias", Age = 13 };
  14:          var Elogan = new Player() { Name = "Elogan", Age = 6 };
  15:   
  16:          var girlsteam = new Team() { 
  17:              Name = "DaGirls", 
  18:              teamtype = TeamType.Soccer, 
  19:              Players = new List<Player>() { Memori, Mikayli, Myranda } 
  20:          };
  21:          var boysteam = new Team() { 
  22:              Name = "DaBoys", 
  23:              teamtype = TeamType.Football, 
  24:              Players = new List<Player>() { Elias, Elogan } 
  25:          };
  26:   
  27:          return new List<Team>() { girlsteam, boysteam };
  28:      }
  29:  }


Here we are implementing the getTeams() method and subsequently calling another method getTeamInfo() to actually populate our class with team and player information to return to the client.

NOTE: Once you have built the service, you will need to make one modification to the configuration file. In the <system.serviceModel> section you will need to change the endpoint binding from the default wsBinding to basicHttpBinding. This allows the service endpoint to be exposed as WS-1 BP 1.1 compliant and use HTTP as the transport for sending SOAP 1.1 messages.

Our next task is to create the Java client.

We can create a new client in NetBeans by selecting New -> Web Service Client from an existing project such as a web application project. When prompted, you select to create the new project by entering a URL to an existing WSDL. Here you enter the URL path to the WSDL that was generated from the WCF service we just created and NetBeans will then create the required proxy code for you. This proxy code will handle all of the data type mappings for you as well as define the generic lists and enums based on the WSDL.

Hint: You will want to enter a package name for the proxy code so that it is encapsulated, otherwise there will be multiple packages the classes based on the WSDL naming schema.

With the proxy code created, we can now write some code in our JSP file to loop through the data returned from the service and display in a browser:

01 <%
02 wcfservice.HelloWorldWCF service = new wcfservice.HelloWorldWCF();
03 wcfservice.IHelloWorldWCF client = service.getBasicHttpBindingIHelloWorldWCF();
04 
05 for (wcfservice.Team team : client.getTeams().getTeam()) {
06     out.println("<br /><b>Team name</b>: " + team.getName() "<br />");
07     out.println("<b>Team type</b>: " + team.getTeamtype().value() "<br /><br />");
08     out.println("Players:<br />");
09 
10     for (wcfservice.Player player : team.getPlayers().getPlayer()) {
11         out.println("&nbsp;&nbsp;Name: <i>" + player.getName() "</i><br />");
12         out.println("&nbsp;&nbsp;Age: <i>" + player.getAge() "</i><br />");
13     }
14 }
15 %>


Here is the sample output from the browser:

 

Team name: DaGirls
Team type: Soccer

Players:
  Name: Memori
  Age: 15
  Name: Mikayli
  Age: 12
  Name: Myranda
  Age: 11

Team name: DaBoys
Team type: Football

Players:
  Name: Elias
  Age: 13
  Name: Elogan
  Age: 6
 


This article is the second in a three part series demonstrating the interoperability between .NET WCF services and Java Web Services.

Article Series:

posted on Tuesday, December 29, 2009 3:50 PM Print
Comments
Gravatar
# re: .NET WCF services and Java Web Services Interoperability - Part 2 of 3
Jerald
5/8/2010 10:59 PM
Great article.

Tried the same example by creating a WCF web service and hosted it in a console application. When created Java web service client using NetBeans by pointing to WSDL, following source files were generated. GetTeams is operation. Why the class is generated? Also, it would be helpful for .NET developers to provide details as to how to proceed from here.


ArrayOfPlayer.java
ArrayOfTeam.java
GetTeams.java
GetTeamsResponse.java
HelloWorldWCF.java
IHelloWorldWCF.java
ObjectFactory.java
Player.java
Team.java
TeamType.java
package-info.java
Gravatar
# re: .NET WCF services and Java Web Services Interoperability - Part 2 of 3
Rajender
6/29/2010 2:09 AM
Hi,
Great article.

I am getting error for UTF - 8 when generating java client for wcf service?
Gravatar
# re: .NET WCF services and Java Web Services Interoperability - Part 2 of 3
Jon
8/14/2011 4:44 PM
I've searching for hours upon hours for something that worked. Great job.
Gravatar
# re: .NET WCF services and Java Web Services Interoperability - Part 2 of 3
sagar
3/14/2013 4:35 AM
this is really useful.. Thanks for sharing your knowledge
Gravatar
# re: .NET WCF services and Java Web Services Interoperability - Part 2 of 3
Abhishek
8/23/2013 2:54 AM
It is a nice article.

Post Comment

Title *
Name *
Email
Comment *  
Verification
Coding strategies for the Java and .Net developer...