The aim of this java web-service tutorial is to write a simple 'Hello World' web service and client in Java. This tutorial will be using the jax-rpc framework in the Glassfish 2 application server. We will create a web-serive and deploy it in Glassfish. Then we will connect to it using a java client. The files and structures are exactly the same as the JWSDP 2 tutorial. In fact you could deploy the war file from that tutorial in Glassfish and it would work fine. Leave any questions or problems as comments and I will endeavour to answer them.
Assumptions
This article assumes that you have Galssfish 2 installed and a domain configured. Please see Installing and Setting up Glassfish 2 for more details. For purposes of this example the glassfish domain is running on port 8082 and the project directory is GlassfishWSHelloWorld.
Versions used in this example
| Sofware/Component | Image |
| Windows XP SP2 | N/A |
| Glassfish 2 | glassfish-installer-v2.1-b60e-windows.jar |
| Java JDK 1.5.0 | N/A |
Although this is a quick 'helloworld' tutorial we will not sacrifice neatness for speed - so all the source files, class files etc will be kept in separate directories and as clean as possible. You will thank me for this later.
Create this directory structure in your working directory- similar to something you would get from eclipse. The structure used in this example is given below. GlassfishWSHelloworld will be referred to as your project or working directory. Create this structure.
GlassfishWSHelloWorld
_|_client_bin
_|_client_src
_|__|_config.xml
_|___GlassfishWSHelloWorldClient.java
_|_web_bin
_|__|_WEB-INF
_|_____|_classes
_|_____|_jaxrpc-ri.xml
_|_____|_web.xml
_|_web_src
_.__|_HelloWorldServiceImplementation.java
_.__|_HelloWorldServiceInterfacejava
The Glassfish app server is installed in the D:\downloads\glassfish directory for this tutorial.
Compiling the webservice code
- Write the code
We don't really need an interface for this example, but I have added one for completeness, because we will use these exact same files for the JWSDP example. In this tutorial the HelloCallEcho function will be exposed.
Save this as HellowWorldServiceInterface.java in your web_src directory.1. package helloworld; 2. 3. import java.rmi.RemoteException; 4. 5. public interface HelloWorldServiceInterface extends java.rmi.Remote{ 6. public String HelloCallEcho(String hellostring)throws java.rmi.RemoteException; 7. public int HelloCallAdd(int helloNumber1, int helloNumber2)throws java.rmi.RemoteException; 8. }
Next, create the implementation
Save this as HelloWorldServiceImplementation.java under in your web_src directory1. package helloworld; 2. 3. public class HelloWorldServiceImplementation implements HelloWorldServiceInterface{ 4. 5. public String HelloCallEcho(String hellostring) { 6. return "Web service echoing back - "+hellostring; 7. } 8. 9. public int HelloCallAdd(int helloNumber1, int helloNumber2) { 10. return helloNumber1 + helloNumber2; 11. } 12. }
`
- Now compile the code.
..workspace\GlassfishWSHelloWorld>javac -d web_bin\WEB-INF\classes web_src\*.java
`
- Open a command prompt and cd into your project directory (GlassfishWSeHelloWorld in this example).Your web_bin directory should have some class file in there.
`
- Create the jaxrpc-ri.xml file in the web_bin\META-INF directory.
1. <?xml version="1.0" encoding="UTF-8"?> 2. <webServices xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/dd" 3. version="1.0" 4. targetNamespaceBase="http://hello.org/wsdl" 5. typeNamespaceBase="http://hello.org/types" 6. urlPatternBase="/ws"> 7. 8. <endpoint name="JWSDPGlassHelloWorld" 9. displayName="Hello World from JWSDP 2" 10. description="This is a Hello World Echo service" 11. wsdl="/WEB-INF/JWSDPGlassHelloWorld.wsdl" 12. interface="helloworld.HelloWorldServiceInterface" 13. implementation="helloworld.HelloWorldServiceImplementation"/> 14. 15. <endpointMapping 16. endpointName="JWSDPGlassHelloWorld" 17. urlPattern="/hello"/> 18. 19. </webServices>
`
- Create the web.xml file in the same WEB-INF directory.
1. <?xml version="1.0" encoding="UTF-8"?> 2. <web-app> 3. <display-name>Hello Service</display-name> 4. <description> 5. Returns a String saying Hello 6. </description> 7. 8. <session-config> 9. <session-timeout>60</session-timeout> 10. </session-config> 11. 12. </web-app>
`
- Cd into web_bin directory and jar it all up using,
..workspace\GlassfishWSHelloWorld\web_bin>jar -cvf HelloWorldServices.jar *
`
- Now use the wsdeploy tool to create the war file that will be deployed to tomcat
..workspace\GlassfishWSHelloWorld\web_bin>d:\downloads\glassfish\bin\wsdeploy.bat -o GlassfishWSHelloWorld.war HelloWorldServices.jar
Deploying the Service
- Open up your broswer and go to the Glassfish admin console. This is http://127.0.0.1:4848 for this tutorial. See Installing and Setting up Glassfish 2 for more details.
`
- Login as admin to the console.
`
- Deploy the GlassfishWSHelloWorld.war application under applications -> web applications -> deploy.
`
- Now navigate to 'http://127.0.0.1:8082/GlassfishWSHelloWorld/hello.' You should see a table with some links. Click on the WSLD link to have a look at the WSDL.
Generating the stub code - required for the client.
- Create the config.xml file in your client_src directory. Wscompile will use this to find the wsdl to create the stubs,
1. <?xml version="1.0" encoding="UTF-8"?> 2. <configuration 3. xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config"> 4. <wsdl 5. location="http://127.0.0.1:8082/GlassfishWSHelloWorld/hello?WSDL" 6. packageName="helloworld"> 7. </wsdl> 8. </configuration>
`
- Open a prompt and cd to your working directory.
`
- Generate the client stub by running wscompile.bat. This should be in your jwsdp install directory under 'jaxrpc\bin.' We use -s option to install all the stub code in the client_src directory.
workspace\GlassfishWSHelloWorld>D:\downloads\glassfish\bin\wscompile.bat -gen -keep -d client_bin -s client_src client_src\config.xml
Compiling the client
- Write the actual client code and save this as GlassfishWSHelloWorldClient.java under the client_src directory.
1. import helloworld.*; 2. 3. public class GlassfishWSHelloWorldClient 4. { 5. public static void main (String args[]) 6. throws Exception 7. { 8. JWSDPGlassHelloWorld_Impl service = new JWSDPGlassHelloWorld_Impl(); 9. HelloWorldServiceInterface stub = service.getHelloWorldServiceInterfacePort(); 10. String response = stub.helloCallEcho("client says hello JWSDP"); 11. System.out.println ( response ); 12. } 13. }
`
- Now compile it using
This should place all the files into the 'client_bin' directory
..workspace\GlassfishWSHelloWorld\client_src>javac -d ..\client_bin -Djava.ext.dirs=D:\downloads\glassfish\lib GlassfishW
SHelloWorldClient.java
Running the application
- CD into client_bin and run the client using the command below,
You should see a printout saying 'hello back' from the web service...workspace\GlassfishWSHelloWorld\client_bin>java -Djava.ext.dirs=D:\downloads\glassfish\lib GlassfishWSHelloWorldClient
Looking at the soap messages using tcpmon
The way the monitor works is like a proxy. Your client connects to the monitor and the monitor connects to the web-service. Remember that Glassfish is running on port 8082
- Go back and edit the HelloworldServiceInterface_Stub.java. This is one of the files generated by wscompile and should be under the client_src\helloworld directory. Around line 40. Change the port to 8083 and save.
original
public HelloWorldServiceInterface_Stub(HandlerChain handlerChain) {
super(handlerChain);
_setProperty(ENDPOINT_ADDRESS_PROPERTY, "http://127.0.0.1:8082/GlassfishWSHelloWorld/hello");
modified
public HelloWorldServiceInterface_Stub(HandlerChain handlerChain) {
super(handlerChain);
_setProperty(ENDPOINT_ADDRESS_PROPERTY, "http://127.0.0.1:8083/GlassfishWSHelloWorld/hello");
`
- Compile the client code as explained previously.
`
- Open up another seprate command prompt and start up tcpmon.
Please refer to the final section in the Setting up Axis2 on Tomcat for more details on this step and why we are using the axis.jar core from Axis 1. This should startup tcpmon.
...\anywhere>java -cp D:\downloads\axis-bin-1_4\axis-1_4\lib\axis.jar org.apache.axis.utils.tcpmon
`
- In the tcpmon gui change the target port from 8080 to 8082, in the listen port type in 8083 and press add.
`
- Go to the 8083 tab at the top.
`
- Now open up a command prompt and run the recompiled client as explained in the previous section- You should see SOAP messages sent back and forth.
Back to the tutorial trail | Home.
1 comments:
Good to see web app creation example from the bottom, so often the IDE covers it up
Post a Comment