SOAP - rFronteddu/general_wiki GitHub Wiki
Simple Object Access Protocol(SOAP) is a network protocol for exchanging structured data between nodes. It uses XML format to transfer messages. It works on top of application layer protocols like HTTP and SMTP for notations and transmission. SOAP allows processes to communicate across platforms, languages, and operating systems since protocols like HTTP are installed on most platforms.
SOAP message transmits some basic information:
- Information about message structure and instructions on processing it.
- Encoding instructions for application-defined data types.
- Information about Remote Procedure Calls and their responses.
The message in XML format contains four parts:
- Envelope: This specifies that the XML message is a SOAP message. A SOAP message is an XML document containing a header and a body, both encapsulated within the envelope. Any fault is included within the body of the message.
- Header(OPTIONAL): It can provide information about the applications.
- Body: This contains the actual message being transmitted. Faults are contained within the body tags.
- Fault(OPTIONAL): It contains the status of the application and any errors. It should not appear more than once in a SOAP message.
Content-Type: application/soap+xml
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header>
<m:GetLastTradePrice xmlns:m="Some-URI" />
</env:Header>
<env:Body>
<symbol xmlns:p="Some-URI" >DIS</symbol>
</env:Body>
</env:Envelope>
- SOAP is a lightweight data interchange protocol based on XML.
- SOAP was designed to be OS and platform-independent (built on top of HTTP which is installed in most systems).
- Proposed by W3 consortium ("governing body" for the Web).
- SOAP is mainly used for Web Services and Application Programming Interfaces (APIs).
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface HelloService {
@WebMethod
String sayHello(String name);
}
### Implement the Web Service
import javax.jws.WebService;
@WebService(endpointInterface = "HelloService") public class HelloServiceImpl implements HelloService { public String sayHello(String name) { return "Hello, " + name + "!"; } }
### Publish the Web Service
import javax.xml.ws.Endpoint;
public class HelloPublisher { public static void main(String[] args) { Endpoint.publish("http://localhost:8080/hello", new HelloServiceImpl()); System.out.println("Service is published at http://localhost:8080/hello?wsdl"); } }
### Client
import javax.xml.namespace.QName; import javax.xml.ws.Service; import java.net.URL;
public class HelloClient { public static void main(String[] args) throws Exception { URL url = new URL("http://localhost:8080/hello?wsdl"); QName qname = new QName("http://impl/", "HelloServiceImplService");
Service service = Service.create(url, qname);
HelloService hello = service.getPort(HelloService.class);
System.out.println(hello.sayHello("Roberto"));
}
}