Showing posts with label Apache Axis. Show all posts
Showing posts with label Apache Axis. Show all posts

April 14, 2011

Auto-generate Client Code with WSDL2Java tool of Apache Axis 1.4

0) We shall create a Web Service client for the web service created in this post by auto-generating code with the WSDL2Java tool of Apache Axis 1.4

Copy the WSDL file from the URL into a text file with the extension *.wsdl and save it in a folder on your hard disk (outside the web application structure), for example, C:\my-wsdl-files

1) Repeat step 3 from the following post - http://www.javaissues.com/2011/04/deploying-wsdd-web-services-with-apache.html

2) Open the command prompt (Start > Run):

- change directory to the C:\my-wsdl-files folder (the folder which contains the saved wsdl file/s)

- issue the following command at the command prompt:
java org.apache.axis.wsdl.WSDL2Java HelloWorld.wsdl

Note: Remove the <?xml ... ?> processing instruction lines from the top of the wsdl file, if present.

3) This will generate the client code in the package: localhost.WebAppName.services.HelloWorld
Copy all the 4 classes into a Java project / application (retain the package name)

4) Create a new class - WSClientTest.java - in the same Java project / application (in which the auto-generated code was added) as follows,


package info.icontraining.wsclient;


import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import localhost.WebAppName.services.HelloWorld.*;


public class WSClientTest
{
    public static void main (String args[]) 
         throws ServiceException, RemoteException {
       
         HelloWorldService service = new HelloWorldServiceLocator(); 
         HelloWorld port = service.getHelloWorld(); 
         System.out.println(port.sayHello());
    }
}


5) Execute the WSClientTest class by running it as a standalone Java Application

Deploying WSDD Web Services with Apache Axis 1.4 - Hello World Example

1) Create a HelloWorld.java service class in the src folder of the web application. The public methods of this class will be exposed as a web service.


package info.icontraining.ws;


public class HelloWorld 

     public String sayHello() 
     { 
         return "Hello World"; 
     } 



2) Create a HelloWorld.wsdd file in a local folder (not inside the application but anywhere on your hard disk, for example, C:\my-wsdd-files) to configure the web service and register with with the Apache Axis deployment


<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> 
   <service name="HelloWorld" provider="java:RPC"> 
      <parameter name="className" value="info.icontraining.ws.HelloWorld"/> 
      <parameter name="allowedMethods" value="*"/> 
   </service> 
</deployment> 


Note: Deploy the application to the Application Server at this time.

3) Register this web service by following the below steps,

a) Go to the "Run" menu in Eclipse and select the "Open Run Dialog..." option

b) In the Project, add the WebAppName; In the Main Class add: org.apache.axis.client.AdminClient

c) Click on the "Arguments" tab and in the Program Arguments, add the following:
-lhttp://localhost:8080/WebAppName/services/AdminService C:\path-to-wsdd-folder\HelloWorld.wsdd

Replace WebAppName and the path-to-wsdd-folder appropriately.

- The web service is registered if you see the output message:
<Admin>Done Processing</Admin>

Note: Re-starting of the JBoss Server will undo the registration of the web service.

4) To access the web service WSDL, invoke the following URL in the browser,
http://localhost:8080/WebAppName/services/HelloWorld?wsdl

April 12, 2011

Apache Axis 1.4 - Hello World JWS Web Service Client

In this post, we will create the Web Service Client for the Apache Axis JWS Web Service at this link

1) Create a class HelloWorldClient.java in the src folder of the web application. This class will be a standalone class with a main method and will run in its own JVM


package info.icontraining.ws.client;


import org.apache.axis.client.*;
import org.apache.axis.encoding.XMLType;
import javax.xml.rpc.ParameterMode;


public class HelloWorldClient {


  public static void main(String[] args) throws Exception{
String endpoint = "http://localhost:8080/WebAppName/HelloWorld.jws";


Service  service = new Service();
Call call = (Call) service.createCall();


call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName( "sayHello" );
call.setReturnType( XMLType.XSD_STRING );


String ret = (String) call.invoke( new Object [] { });
System.out.println("Got result : " + ret);
  }
}


2) Run this class as a 'Java Application' (and will not execute on the server)

Apache Axis 1.4 - Hello World Example JWS web service

1) Download the following resources:
Apache Axis Jar Files - Click here
WebContent Files - Click here
Resources Files - Click here

2) Unzip the Axis_jars.zip file and copy the Axis jars to the WebContent/WEB-INF/lib folder of the Web Application.
Unzip the web_content_files.zip file and copy the contents to the WebContent folder of the Web Application.
In the src folder create a new package named 'resources'. Unzip the resources_files.zip file and copy the contents to the src folder within the package 'resources'.

3) Add the following configuration to the WebContent/WEB-INF/web.xml file of the Web Application


<listener>
   <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
</listener>



<servlet>
    <servlet-name>AxisServlet</servlet-name>
    <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
</servlet>


<servlet>
    <servlet-name>AdminServlet</servlet-name>
    <servlet-class>org.apache.axis.transport.http.AdminServlet</servlet-class>
    <load-on-startup>100</load-on-startup>
</servlet>


<servlet>
    <servlet-name>SOAPMonitorService</servlet-name>
    <servlet-class>org.apache.axis.monitor.SOAPMonitorService</servlet-class>
    <init-param>
      <param-name>SOAPMonitorPort</param-name>
      <param-value>5001</param-value>
    </init-param>
    <load-on-startup>100</load-on-startup>
</servlet>



<servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>


<servlet-mapping>
    <servlet-name>AdminServlet</servlet-name>
    <url-pattern>/servlet/AdminServlet</url-pattern>
</servlet-mapping>


<servlet-mapping>
    <servlet-name>SOAPMonitorService</servlet-name>
    <url-pattern>/SOAPMonitor</url-pattern>
</servlet-mapping>


<mime-mapping>
    <extension>wsdl</extension>
    <mime-type>text/xml</mime-type>
</mime-mapping>


<mime-mapping>
    <extension>xsd</extension>
    <mime-type>text/xml</mime-type>
</mime-mapping>

4) Test the Axis setup by typing the following URL in the browser:

http://localhost:8080/WebAppName/happyaxis.jsp

5) To deploy a JWS Web Service, create a file, HelloWorld.jws, in the WebContent folder of the web application with the following code

public class HelloWorld { 
  
  public String sayHello() { 
     return "Hello World"; 
  } 


6) Deploy the Web Application and access the following URL in the browser:

http://localhost:8080/WebAppName/HelloWorld.jws

To access the WSDL for the web service, type the following URL in the browser:

http://localhost:8080/WebAppName/HelloWorld.jws?wsdl


Resolution to the Error:




java.lang.RuntimeException: No compiler found in your classpath! (you may need to add 'tools.jar')

Copy the tools.jar file from the JDK/lib folder into the %JBOSS HOME%\server\default\lib folder.