JAX-WS Document style:
A Document style SOAP message body contains an XML document that can be validated against a defined XML schema. It is a more customizable and flexible approach as the protocol relies on the pre-defined schema to determine the structure of the SOAP message. That means we are free to customize the SOAP messages as much as we want.
First create a Web Service Endpoint Interface which will contains the declarations of all the methods we want to include in the Web Service. Create a class which provides the implementation of Endpoint interface. Finally create an Endpoint publisher which deploys the web service and creates and publishes the endpoint at the specified address.
Note: We have to run our Endpoint publisher to make Web Service available to clients. The @SOAPBinding is optional in case of Document Style because the default style is document. JAX-WS API is inbuilt in JDK.
Web Service:
IHelloWorld.java
import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; /** * Service Endpoint Interface * @author w3schools */ @WebService @SOAPBinding(style = Style.DOCUMENT) public interface IHelloWorld { @WebMethod String sayHello(String name); } |
HelloWorld.java
import javax.jws.WebService; /** * Service Endpoint Implementation * @author w3schools */ @WebService(endpointInterface = "com.w3schools.business.IHelloWorld") public class HelloWorld implements IHelloWorld { @Override public String sayHello(String name) { return "JAX-WS Document Style. Hello " + name; } } |
HelloWorldPublisher.java
import javax.xml.ws.Endpoint; public class HelloWorldPublisher { public static void main(String args[]){ Endpoint.publish("http://localhost:8080/ws/helloWorldDocument", new HelloWorld()); } } |
Note:
When you run the end point publisher, We will get the following error message:
Wrapper class com.w3schools.business.jaxws.SayHello is not found. Have you run APT to generate them? |
To solve this problem, we have to go to bin directory of our current project in command prompt and run the following command.
wsgen -keep -cp . com.w3schools.business.HelloWorld |
It will generate two following two classes:
SayHello.java
package com.w3schools.business.jaxws; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "sayHello", namespace = "http://business.w3schools.com/") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "sayHello", namespace = "http://business.w3schools.com/") public class SayHello { @XmlElement(name = "arg0", namespace = "") private String arg0; /** * * @return * returns String */ public String getArg0() { return this.arg0; } /** * * @param arg0 * the value for the arg0 property */ public void setArg0(String arg0) { this.arg0 = arg0; } } |
sayHelloResponse.java
package com.w3schools.business.jaxws; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "sayHelloResponse", namespace = "http://business.w3schools.com/") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "sayHelloResponse", namespace = "http://business.w3schools.com/") public class SayHelloResponse { @XmlElement(name = "return", namespace = "") private String _return; /** * * @return * returns String */ public String getReturn() { return this._return; } /** * * @param _return * the value for the _return property */ public void setReturn(String _return) { this._return = _return; } } |
Web Service Client:
HelloWorldWSClient.java
import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class HelloWorldWSClient { public static void main(String args[]){ try { URL url=new URL("http://localhost:8080/ws/helloWorldDocument?wsdl"); QName qname = new QName("http://business.w3schools.com/", "HelloWorldService"); Service service = Service.create(url, qname); IHelloWorld helloWorld = service.getPort(IHelloWorld.class); System.out.println(helloWorld.sayHello("w3schools")); } catch (MalformedURLException e) { e.printStackTrace(); } } } |
How it works:
First, client send a wsdl request to service endpoint (http://localhost:8080/ws/helloWorldDocument?wsdl) and server send the following response.
This XML file does not appear to have any style information associated with it. The document tree is shown below. <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. --> <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. --> <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://business.w3schools.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://business.w3schools.com/" name="HelloWorldService"> <types> <xsd:schema> <xsd:import namespace="http://business.w3schools.com/" schemaLocation="http://localhost:8080/ws/helloWorldDocument?xsd=1"/> </xsd:schema> </types> <message name="sayHello"> <part name="parameters" element="tns:sayHello"/> </message> <message name="sayHelloResponse"> <part name="parameters" element="tns:sayHelloResponse"/> </message> <portType name="IHelloWorld"> <operation name="sayHello"> <input message="tns:sayHello"/> <output message="tns:sayHelloResponse"/> </operation> </portType> <binding name="HelloWorldPortBinding" type="tns:IHelloWorld"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <operation name="sayHello"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="HelloWorldService"> <port name="HelloWorldPort" binding="tns:HelloWorldPortBinding"> <soap:address location="http://localhost:8080/ws/helloWorldDocument"/> </port> </service> </definitions> |
A second call, client put method invoke request in SOAP envelope and send it to service endpoint. At the service endpoint, call the requested method and put the result in a SOAP envelope and send it back to client.
Hello w3schools |