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

Part III - Create the Java Web Service consumed by a .NET client

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

NOTE: If you haven't read Part I or Part II, you should review them before continuing on.

In Part I we reviewed the class diagram of the Team class and in Part II we covered creating a WCF service that exposed that class and consumed it with a Java client. Now we will be creating the JWS and consume it with a .NET client.

We will start with creating the Player class:

01 package jws;
02 
03 public class Player {
04 
05     private String _name;
06     private int _age;
07 
08     public String getName() {
09         return _name;
10     }
11 
12     public void setName(String name) {
13         _name = name;
14     }
15 
16     public int getAge() {
17         return _age;
18     }
19 
20     public void setAge(int age) {
21         _age = age;
22     }
23 }


Here we have the Player class that contains Name and Age properties that are exposed using the Java Bean "getter/setter" convention.

Now we will create the TeamType enumerator:

1 package jws;
2 
3 public enum TeamType {
4     Soccer,
5     Football
6 }


This allows us to limit the types of teams returned to only Soccer and Football.

The next step is to create the Team class:

01 package jws;
02 
03 import java.util.*;
04 
05 public class Team {
06 
07     private String _name;
08     private TeamType _teamtype;
09     private List<Player> _players;
10 
11     public String getName() {
12         return _name;
13     }
14 
15     public void setName(String name) {
16         _name = name;
17     }
18 
19     public TeamType getTeamType() {
20         return _teamtype;
21     }
22 
23     public void setTeamType(TeamType teamtype) {
24         _teamtype = teamtype;
25     }
26 
27     public List<Player> getPlayers() {
28         return _players;
29     }
30 
31     public void setPlayers(List<Player> players) {
32         _players = players;
33     }
34 }


Here you can see now that we have included in the Team class a Name property, a TeamType property based off our enum above, and finally a generic list of Player objects. Again these are all exposed using the getter/setter convention.

Now that our Team class is complete, our next step is to create the Java Web Service:

01 package jws;
02 
03 import javax.jws.WebService;
04 import java.util.*;
05 import java.util.ArrayList;
06 
07 @WebService()
08 public class HelloWorldJWS {
09 
10     public List<Team> getTeams() {
11         return GetTeamInfo();
12     }
13 
14     private List<Team> GetTeamInfo() {
15 
16         Player Memori = CreatePlayer("Memori"15);
17         Player Mikayli = CreatePlayer("Mikayli"12);
18         Player Myranda = CreatePlayer("Myranda"11);
19         Player Elias = CreatePlayer("Elias"13);
20         Player Elogan = CreatePlayer("Elogan"6);
21 
22         List<Player> girlplayers = new ArrayList<Player>();
23         List<Player> boyplayers = new ArrayList<Player>();
24 
25         girlplayers.add(Memori);
26         girlplayers.add(Mikayli);
27         girlplayers.add(Myranda);
28         boyplayers.add(Elias);
29         boyplayers.add(Elogan);
30 
31         Team dagirls = new Team();
32 
33         dagirls.setName("DaGirls");
34         dagirls.setTeamType(TeamType.Soccer);
35         dagirls.setPlayers(girlplayers);
36 
37         Team daboys = new Team();
38 
39         daboys.setName("DaBoys");
40         daboys.setTeamType(TeamType.Football);
41         daboys.setPlayers(boyplayers);
42 
43         List<Team> teams = new ArrayList<Team>();
44 
45         teams.add(dagirls);
46         teams.add(daboys);
47 
48         return teams;
49     }
50 
51     private Player CreatePlayer(String name, int age) {
52 
53         Player newplayer = new Player();
54 
55         newplayer.setName(name);
56         newplayer.setAge(age);
57 
58         return newplayer;
59     }
60 }


Like our WCF service, our JWS returns one method getTeams() that returns a generic list of Team objects. The GetTeamInfo() method is used to create some test teams and players.

The final step is to create the .NET client. You can either use the svcutil.exe utility or Visual Studio (add reference wizard) to create the proxy based on the JWS WSDL.

 
   1:  JWS.HelloWorldJWSClient webclient = new JWS.HelloWorldJWSClient();
   2:   
   3:  JWS.team[] Teams = webclient.getTeams();
   4:   
   5:  foreach (JWS.team team in Teams)
   6:  {
   7:     jws.Text += "<br /><b>Team name</b>: " + team.name + "<br />";
   8:     jws.Text += "<b>Team type</b>: " + team.teamType + "<br /><br />";
   9:     jws.Text += "Players:<br />";
  10:   
  11:     foreach (JWS.player player in team.players)
  12:     {
  13:         jws.Text += "&nbsp;&nbsp;Name: <i>" + player.name + "</i><br />";
  14:         jws.Text += "&nbsp;&nbsp;Age: <i>" + player.age.ToString() + "</i><br />";
  15:     }
  16:  }


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 third in a three part series demonstrating the interoperability between .NET WCF services and Java Web Services.

Article Series:

posted on Wednesday, December 30, 2009 9:32 AM Print
Comments
Gravatar
# re: .NET WCF services and Java Web Services Interoperability - Part 3 of 3
Albert
10/17/2011 7:03 AM
really good explanation! it explain everything in the code, easy to undertand with a example made step by step.

Thanks!
Gravatar
# re: .NET WCF services and Java Web Services Interoperability - Part 3 of 3
Darshan Altekar
3/11/2013 1:35 AM
Hi,

In my WCF client if I add some security elements (e.g. adding message level security , including timestamp in the header) will these setting will take effect on the messages that are sent to JAVA web service?

I have a WCF client with me. Through this client I'm talking with a .net web service and java web service. I have added messageLogging element to the app.config file so that message that are sent or received will get logged. What I'm observing is only the messages that are sent/received from .net web service are logged. No message sent/received from Java web service is logged. Any help will be really appreciated.
Gravatar
# re: .NET WCF services and Java Web Services Interoperability - Part 3 of 3
Ashok
10/6/2015 9:23 AM
can u please explain one doubt
i am using dyanamically wcf rest service in my application ,in the url if i pass the java web service url , will it work
Gravatar
# re: .NET WCF services and Java Web Services Interoperability - Part 3 of 3
Nicholas
8/10/2017 9:31 AM
To-day, I really liked the post about concept which supports the extremely essential topic. The article on this helped me to to publish an essay. But ordinary I acquire it here cheap assignment
Gravatar
# re: .NET WCF services and Java Web Services Interoperability - Part 3 of 3
Bying
10/12/2017 12:20 AM
1 package jws;
2
3 public enum TeamType {
4 Soccer,
5 Football
hotmail login}
}
Gravatar
# About Thread
Lary Brown
10/29/2017 2:58 AM
Nice Post But more importantly I think that hard work is making your blog more awesome, wonderful Blog. Keep this Blog Up. Hotmail Login
Gravatar
# re: .NET WCF services and Java Web Services Interoperability - Part 3 of 3
happy wheels
11/2/2017 6:27 AM
Just a smile and the rain is gone Can hardly believe it, yeah. There's an angel standing next to me. Reaching for my heart Just a smile and there's no way back .Can hardly believe it, yeah But there's an angel calling me. Reaching for my heart I know that I'll be okay now. This time, it's real I lay my love on you It's all I wanna do Every time I breathe I feel brand new You open up my heart Show me all your love and walk right through As I lay my love on you. happy wheels

Gravatar
# Thanks for sharing.
thuylien
11/8/2017 11:05 PM
The article is great, there is a lot of useful information. Thank the author very much.
free online movies site

Post Comment

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