Showing posts with label SOAP. Show all posts
Showing posts with label SOAP. Show all posts

About Java Web Services with Axis and JAX-RPC in Eclipse




Follow the steps for creating he simple Axis and JAX-RPC, Service and Client code with Examples:

Step 1. List of software required for this application if not found in your system, install it:

Step 2. Extract the xml-axis-rc1-bin.zip into your local system

  • Navigate till \xml-axis-rc1-bin\axis-1_0\webapps and copy the axis folder

Step 3. Pest the axis folder (Already copy in above step) into Tomcate x.x \webapps

Step 4. Restart your Tomcate x.x

Step 5. Open the browser and type the http://localhost:port/axis

Note: if tools.jar file is not there in xml-axis-rc1-bin\axis-1_0\webapps\axis\WEB-INF\lib then copy from \jdk1.6.0_26\lib.

Now environment is configured on and off, need service & client code.

Step 6. Server Side Code

//File name : TestServerSideCode.jws save the file ext with jws insteed of java
public class TestServerSideCode{
public String testServerSideCode()
{
return "Working";
}
}

Step 7. Save TestServerSideCode.jws file into \Tomcat x.x\webapps\axis folder

Step 8. Restart the Tomcate x.x

Open the browser and invoke the http://localhost:port/axis/TestServerSideCode.jws?wsdl

Step 9. Client Side code with Dynamic Invocation Interface (DII)

TestClientSideCode.java

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;

public class TestClientSideCode
{
public static void main(String [] args) {
try {
String endPointURL = "http://localhost:8090/axis/TestServerSideCode.jws";

Service service = new Service();
Call call = (Call) service.createCall();
call.setOperationName(new QName("http://localhost:8090/axis/TestServerSideCode.jws?wsdl", "testServerSideCode"));
call.setTargetEndpointAddress( new java.net.URL(endPointURL) );

String response = (String) call.invoke( new Object[] {} );

System.out.println(" Test data " + response);
} catch (Exception e) {
System.err.println("Exception: " + e);
}
}
}


Step 10. Save the TestClientSideCode.java in your local drive e.g C:\Blogs\AxisWithJAXRPC

Step 11. Open the cmd prompt, type cd then C:\Blogs\AxisWithJAXRPC

Step 12. Copy and pest the below command in command prompt with above steps

set AXIS_HOME=C:\Blogs\xml-axis-rc1-bin\axis-1_0

set CLASSPATH=.;%AXIS_HOME%\lib\axis.jar;%AXIS_HOME%\lib\axis-ant.jar;%AXIS_HOME%\lib\commons-discovery.jar;%AXIS_HOME%\lib\commons-logging.jar;%AXIS_HOME%\lib\jaxrpc.jar;%AXIS_HOME%\lib\saaj.jar;%AXIS_HOME%\lib\wsdl4j.jar;%AXIS_HOME%\lib\log4j-1.2.4.jar

Note: Change the location of xml-axis-rc1-bin\axis-1_0 in your system

Step 13. Compile the TestClientSideCode.java file with C:\Blogs\AxisWithJAXRPC>javac TestClientSideCode.java

Step 14. Run the same C:\Blogs\AxisWithJAXRPC>java TestClientSideCode

You will get out put as Test data Working



Done form my end, must try and do at your end and do let me know if any concerns.

Happy learning and implementation!!!

About JAX-WS (Java API for XML Web Service) as Web Archive WAR in Eclipse deployed on Tomcat

This tutorial, we will discussed more about, JAX-WS (Java API for XML Web Service) as Web Archive WAR in Eclipse deployed on Tomcat.

Software/Hardware requirement:
Eclips
Java 1.6+
Tomcat 6+

Create Dynamic web project in eclipse
Open the eclips from desktop or from existing dire.

Click on the File > New > Dynamic Web Project > Next.
Enter the Project name: jaxwsprj
Click Next > Click Next > Finish
Let be context root and content directory as it is default or change as per your convenient.

Create Web Service Interface

Right click on the src folder located under Java Resources > Next > New > class

Enter the name: GreetingWebService
Package: com.vinod.webservicejaxws

Click on the Finish

GreetingWebService.java

package com.vinod.webservicejaxws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

/**
* @author Vinod Kumar
* Web Service endpoint interface
*/

@WebService
@SOAPBinding(style = Style.RPC)
public interface GreetingWebService{

/**
* sayWelcome with parameter as string
*/
@WebMethod String sayWelcome(String sName);
}



Create Web Service Implementation
Right click on the src folder located under Java Resources > Next > New > class
Enter the name: GreetingWebServiceImpl
Package: com.vinod.webservicejaxws

Click on the Finish

package com.vinod.webservicejaxws;

import javax.jws.WebService;

import com.vinod.webservicejaxws.GreetingWebService;

/**
*
* @author Vinod Kumar
* This is service implementation class
*/

@WebService(endpointInterface = "com.vinod.webservicejaxws.GreetingWebService")
public class GreetingWebServiceImpl implements GreetingWebService {

@Override
public String sayWelcome(String sName) {
return "Welcome " + sName + " !";
}
}


Add the sun-jaxws.xml

Right click on the WEB-INF folder located under WebContent > File
name of file: sun-jaxws.xml
This is for supporting the jax ws compilation.

< ?xml version="1.0" encoding="UTF-8"?>
< endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint name="GreetingWebService" implementation="com.vinod.webservicejaxws.impl"
url-pattern="/GreetingWebService" />

< /endpoints>

Modify the web.xml

Just modify the web.xml file as mention bellow:

< ?xml version="1.0" encoding="UTF-8"?>

< web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jaxwsprj</display-name>

<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>GreetingWebService</servlet-name>
<servlet-class>
com.sun.xml.ws.transport.http.servlet.WSServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GreetingWebService</servlet-name>
<url-pattern>/GreetingWebService</url-pattern>
</servlet-mapping>
< /web-app>

Download the jax-ws dependent jars

Copy these jar in /tomcat/lib from http://jax-ws.java.net/

Export the WAR in Tomcat
Right click on the jaxwsprj project > Export > War file


About JAX-WS Java API for XML Web Service as Web Archive WAR on Tomcat, Example, Sample, Tutorial, Step by Step, Eclipse, Tomcat and Code


Start the tomcat from /tomcate/bin/startup.bat

Verify the service
Open the any browser and enter the URL
http://localhost:8080/jaxwsprj/GreetingWebService

Note: Where as tomcat port is running on 8080



Click on link, wsdl will display as below:

< definitions
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"
xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://webservicejaxws.vinod.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservicejaxws.vinod.com/"
name="GreetingWebServiceImplService">
<types />
<message name="sayWelcome">
<part name="arg0" type="xsd:string" />
</message>
<message name="sayWelcomeResponse">
<part name="return" type="xsd:string" />
</message>
<portType name="GreetingWebService">
<operation name="sayWelcome">
<input
wsam:Action="http://webservicejaxws.vinod.com/GreetingWebService/sayWelcomeRequest"
message="tns:sayWelcome" />
<output
wsam:Action="http://webservicejaxws.vinod.com/GreetingWebService/sayWelcomeResponse"
message="tns:sayWelcomeResponse" />
</operation>
</portType>
<binding name="GreetingWebServiceImplPortBinding" type="tns:GreetingWebService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="rpc" />
<operation name="sayWelcome">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" namespace="http://webservicejaxws.vinod.com/" />
</input>
<output>
<soap:body use="literal" namespace="http://webservicejaxws.vinod.com/" />
</output>
</operation>
</binding>
<service name="GreetingWebServiceImplService">
<port name="GreetingWebServiceImplPort" binding="tns:GreetingWebServiceImplPortBinding">
<soap:address location="http://localhost:8080/jaxwsprj/GreetingWebService" />
</port>
</service>
< /definitions>

Finishing Word:
Thanks! Put your input if any. Like and share it, if my work looks good.
Happy learning and implementation.

About Simple Object Access Protocol (SOAP)

Simple Object Access Protocol (SOAP) means various things to different people:
• It's a wire protocol.
• It's an RPC mechanism.
• It's an interoperability standard.
• It's a document exchange protocol.
• It's a universal business-to-business communications language.
• SimpleObject Access Protocol – http://www.w3c.org/TR/SOAP/
• A lightweight protocol for exchange of information in a decentralized, distributed environment.
• Two different styles to use: – to encapsulate RPC calls using the extensibility and flexibility
of XML. – to deliver a whole document without any method calls encapsulated




XML messaging using SOAP

About Simple Object Access Protocol (SOAP), SOAP Request Envelope, SOAP Response Envelope, SOAP Structure, Adding Header, SOAP Headers, SOAP Body, SOAP RPC Example, Sample, Basic, Code, Tutorials and Examples.
Add caption


SOAP specification

• The SOAP specification describes four major components: Formatting conventions for encapsulating data. Ex: SOAP envelope, header, body etc., Routing directions in the form of an envelope, a transport or protocol binding, Ex: SOAP sender , receiver etc., Encoding rules An RPC mechanism
• The envelope defines a convention for describing the contents of a message, which in turn has implications on how it gets processed.
• A protocol binding provides a generic mechanism for sending a SOAP envelope via a lower level protocol such as HTTP.
• Encoding rules provide a convention for mapping various application data types into an XML tag-based representation.
• Finally, the RPC mechanism provides a way to represent remote procedure calls and their return values.


SOAP Message Structure

About Simple Object Access Protocol (SOAP), SOAP Request Envelope, SOAP Response Envelope, SOAP Structure, Adding Header, SOAP Headers, SOAP Body, SOAP RPC Example, Sample, Basic, Code, Tutorials and Examples.

Block structure of a SOAP envelope

Block structure of a SOAP envelope

SOAP Request Envelope:
< soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://emp" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<q0:getName>
<q0:sName>Vinod Kumar</q0:sName>
</q0:getName>
</soapenv:Body>
< /soapenv:Envelope>

SOAP Response Envelope:
< soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<getNameResponse xmlns="http://emp">
<getNameReturn>Welcome Vinod Kumar</getNameReturn>
</getNameResponse>
</soapenv:Body>
< /soapenv:Envelope>

SOAP Structure
• A SOAP message is contained in an envelop.
• The envelop element in turn contain (in order)
– An optional header with one or more child entries.
– A body element that can contain one or more child entries.
These child entries may contain arbitrary XML data.

Adding Header
< SOAP-ENV:Header>
<jaws:MessageHeader xmlns:jaws="urn:training-samples">
<From>Me</From>
<To>You</To>
<MessageId>9999</MessageId>
</jaws:MessageHeader>
< /SOAP-ENV:Header>

SOAP Headers
• Headers are really just extension points where you can include elements from other namespaces.
– i.e., headers can contain arbitrary XML.
• Header entries may optionally have a “mustUnderstand” attribute.
– mustUnderstand=1 means the message recipient must process the header element.
– If mustUnderstand=0 or is missing, the header element is optional.

SOAP Body
Body entries are really just placeholders for arbitrary XML from some other namespace.
• The body contains the XML message that you are transmitting.
• The message format is not specified by SOAP.
– The <Body></Body> tag contains actual XML message.
– The recipient decides what to do with the message.
e.g:-
<soapenv:Body>
<getNameResponse xmlns="http://emp">
<getNameReturn>Welcome Vinod Kumar</getNameReturn>
</getNameResponse>
</soapenv:Body>

SOAP RPC Example
A simple example
– Calls with a string public String hello(String name)
– Returns a greeting “Hi!” + name

SOAP RPC Example


Reference:
wiki-SOAP