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 += " Name: <i>" + player.name + "</i><br />";
14: jws.Text += " 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: