Monday, 29 April 2013

Java Web Service HelloWorld (Document Style)

Java Web Service HelloWorld (Document style) Example

I will show you how to use JAX-WS to create a SOAP-based web service (document style) endpoint, compare it with RPC style,
It need to do some extra efforts to work it.

There are some steps for creating web service HelloWorld (Document style) Example.
Step 1 : Create a Web Service Endpoint Interface

package com.test.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.DOCUMENT, use=Use.LITERAL) //optional
public interface HelloWorld{
@WebMethod String SayHello(String name);
}
Note : In JAX-WS development, convert from 'RPC style' to 'Document style' is very easy,
only change the @SOAPBinding style option.

Step 2 : Create a Web Service Endpoint Implementation
package com.test.ws;
import javax.jws.WebService;
@WebService(endpointInterface = "com.test.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
@Override
public String SayHello(String name) {
return "Hello JAX-WS " + name;
}

}

Step 3 : Create a Endpoint Publisher

package com.test.endpoint;
import javax.xml.ws.Endpoint;
import com.test.ws.HelloWorldImpl;
//Endpoint publisher
public class HelloPublisher{
public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
}
}
when you run the publisher, you get the following error message
Wrapper class com.test.ws.jaxws.SayHello is not found.
Have you run APT to generate them?

Step 4 : Run wsgen command to generate the required artifact
Document style requires extra classes to run, you can use 'wsgen tool to generate all necessary Java artifacts
(mapping classes, wsdl or xsd schema). The 'wsgen' command is required to read a service endpoint
implementation class.

wsgen -keep -cp . com.test.ws.HelloWorldImpl

The above command will generates two classes, copy it to your com.test.ws.jaxws folder.

The *****SayHello.java**** File

package com.test.ws.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://ws.test.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayHello", namespace = "http://ws.test.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;
}
}
The *****SayHelloResponse.java**** File

package com.test.ws.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://ws.test.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayHelloResponse", namespace = "http://ws.test.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;
}
}

It is done you can access URL as http://localhost:9999/ws/hello?wsdl
Step 5 :Web Service Client
Create a web service client to access your published service as

package com.test.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.test.ws.HelloWorld;

public class HelloClient{
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:9999/ws/hello?wsdl");
QName qname = new QName("http://ws.test.com/", "HelloWorldImplService");
Service service = Service.create(url, qname);
HelloWorld hello = service.getPort(HelloWorld.class);
System.out.println(hello.sayHello("Wakil"));
}
}

No comments:

Post a Comment