Soap:
SOAP stands for Simple Object Access Protocol.
SOAP provides a way to communicate between applications running on different operating systems, with different technologies and programming languages.
The best way to communicate between applications is over HTTP, because HTTP is supported by all Internet browsers and servers. SOAP was created to accomplish this
Notice: Soap uses only XML data format for request and response.
Basic Requirements:
1. Eclipse
2. Jboss Server
3. Jdk 1.6 or +
4. Following Lib:
axis.jar
commons-discovery-0.2.jar
commons-logging.jar
jaxrpc.jar
saaj.jar
wsdl4j.jar
Put these jars in your lib folder.
Project Structure in Eclipse:
Let me discuss about above files.
1. RequestXML.java : This is request pojo class which takes one parameter which has XML format.
package com.soap.test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "RequestXMl", propOrder = { "id"})
public class RequestXMl {
@XmlElement(name = "Id")
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
2. ResponseXML.java: This is response pojo class which return name and age which has XML format.
package com.soap.test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ResponseXML", propOrder = { "name", "age"})
public class ResponseXML {
@XmlElement(name = "Name")
private String name;
@XmlElement(name = "Age")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3. SoapDAO.java: This is the DAO(Data Access Object) class as we all java developer know how to write DAO, handler, controller class, So here i am directly calling dao class from service class you can have multiple layer based on your company's project structure.
package com.soap.test;
public class SoapDAO {
public ResponseXML getRecord(RequestXMl req )
{
ResponseXML res=new ResponseXML();
System.out.println("Request ID: "+req.getId());
res.setAge(27);
res.setName("Lokesh Sahu");
return res;
}
}
4. SoapService.java: This is main class for soap service you can have any name of the class in my case name of the service is SaopService.
In this class as of now we have only one service public ResponseXML getDetails(RequestXMl req ) in this class
You can have multiple methods in same service class.
In below Class if you see two line of code just above class name which has red text that is the only code which define its a soap service, only first class or we can say the entry class of the request is soap service.
Some keyword which is used in soap services are following:
targetNamespace: this is just reverse of your package name
serviceName: This is the class name which will be published as service name
portName: portname should be services name + SOAP keyword as we follow standards
package com.soap.test;
import javax.jws.soap.SOAPBinding;
import org.xml.sax.XMLReader;
@javax.jws.WebService(targetNamespace = "http://test.soap.com/", serviceName = "SoapService", portName = "SoapServiceSOAP")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public class SoapService {
public ResponseXML getDetails(RequestXMl req )
{
return new SoapDAO().getRecord(req);
}
}
Now all setup its time to build project and create war file,
Right click on project and export war and save it on any location.
Now start the Jboss Server and Deploy this war.
While deploying Jboss will give you service url as given below image
In Image you can see Red circle which has service url with
http://localhost:4000/SoapTest/SoapService
in my case the url which has machine name but it will be depends on machine
Now copy the URL and paste in google chrome and add ?wsdl in last with url and press enter like this :
http://localhost:4000/SoapTest/SoapService?wsdl
So it will give you wsdl file, you can give this wsdl file to anyone who want to consume your services.
For local testing you should have SOAPUI 4.0+ software to test
In my SOPA ui how it look like see the below image
SOAP stands for Simple Object Access Protocol.
SOAP provides a way to communicate between applications running on different operating systems, with different technologies and programming languages.
The best way to communicate between applications is over HTTP, because HTTP is supported by all Internet browsers and servers. SOAP was created to accomplish this
Notice: Soap uses only XML data format for request and response.
Basic Requirements:
1. Eclipse
2. Jboss Server
3. Jdk 1.6 or +
4. Following Lib:
axis.jar
commons-discovery-0.2.jar
commons-logging.jar
jaxrpc.jar
saaj.jar
wsdl4j.jar
Put these jars in your lib folder.
Project Structure in Eclipse:
Let me discuss about above files.
1. RequestXML.java : This is request pojo class which takes one parameter which has XML format.
package com.soap.test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "RequestXMl", propOrder = { "id"})
public class RequestXMl {
@XmlElement(name = "Id")
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
2. ResponseXML.java: This is response pojo class which return name and age which has XML format.
package com.soap.test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ResponseXML", propOrder = { "name", "age"})
public class ResponseXML {
@XmlElement(name = "Name")
private String name;
@XmlElement(name = "Age")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3. SoapDAO.java: This is the DAO(Data Access Object) class as we all java developer know how to write DAO, handler, controller class, So here i am directly calling dao class from service class you can have multiple layer based on your company's project structure.
package com.soap.test;
public class SoapDAO {
public ResponseXML getRecord(RequestXMl req )
{
ResponseXML res=new ResponseXML();
System.out.println("Request ID: "+req.getId());
res.setAge(27);
res.setName("Lokesh Sahu");
return res;
}
}
4. SoapService.java: This is main class for soap service you can have any name of the class in my case name of the service is SaopService.
In this class as of now we have only one service public ResponseXML getDetails(RequestXMl req ) in this class
You can have multiple methods in same service class.
In below Class if you see two line of code just above class name which has red text that is the only code which define its a soap service, only first class or we can say the entry class of the request is soap service.
Some keyword which is used in soap services are following:
targetNamespace: this is just reverse of your package name
serviceName: This is the class name which will be published as service name
portName: portname should be services name + SOAP keyword as we follow standards
package com.soap.test;
import javax.jws.soap.SOAPBinding;
import org.xml.sax.XMLReader;
@javax.jws.WebService(targetNamespace = "http://test.soap.com/", serviceName = "SoapService", portName = "SoapServiceSOAP")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public class SoapService {
public ResponseXML getDetails(RequestXMl req )
{
return new SoapDAO().getRecord(req);
}
}
Now all setup its time to build project and create war file,
Right click on project and export war and save it on any location.
Now start the Jboss Server and Deploy this war.
While deploying Jboss will give you service url as given below image
in my case the url which has machine name but it will be depends on machine
Now copy the URL and paste in google chrome and add ?wsdl in last with url and press enter like this :
http://localhost:4000/SoapTest/SoapService?wsdl
So it will give you wsdl file, you can give this wsdl file to anyone who want to consume your services.
For local testing you should have SOAPUI 4.0+ software to test
In my SOPA ui how it look like see the below image
You can configure the service url in soap ui to test in local you can also search in google how to import or create new project in soap ui for testing.
Thanks A Lot for reading my Blog, Happy Learning.
You can also visit my REST and Spring MVC blog on same url.