package com.dineshonjava.ws; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; /** * @author Dinesh Rajput * Service Endpoint Interface */ @WebService @SOAPBinding(style = Style.RPC) public interface HelloWorld { @WebMethod String sayHelloWorld(); }Create a Web Service Endpoint Implementation
package com.dineshonjava.ws; import javax.jws.WebService; /** * @author Dinesh Rajput * Service Implementation Bean */ @WebService(endpointInterface = "com.dineshonjava.ws.HelloWorld") public class HelloWorldImpl implements HelloWorld { @Override public String sayHelloWorld() { return "Hello World!! Dinesh on Java!!"; } }Create a Endpoint Publisher-
package com.dineshonjava.endpoint; import javax.xml.ws.Endpoint; import com.dineshonjava.ws.HelloWorldImpl; /** * @author Dinesh Rajput * Endpoint publisher */ public class HelloWorldPublisher { /** * @param args */ public static void main(String[] args) { Endpoint.publish("http://localhost:8181/sdnext/hello", new HelloWorldImpl()); } }2. sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?> <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0"> <endpoint name="HelloWorld" implementation="com.dineshonjava.ws.HelloWorldImpl" url-pattern="/hello"/> </endpoints>When user access /hello/ URL path, it will fire the declared web service, which is HelloWorldImpl.java.
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>WebService</display-name> <listener> <listener-class> com.sun.xml.ws.transport.http.servlet.WSServletContextListener </listener-class> </listener> <servlet> <servlet-name>hello</servlet-name> <servlet-class> com.sun.xml.ws.transport.http.servlet.WSServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <session-config> <session-timeout>120</session-timeout> </session-config> </web-app>4. WAR Content
package com.dineshonjava.client; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import com.dineshonjava.ws.HelloWorld; /** * @author Dinesh Rajput * */ public class HelloWorldClient { /** * @param args */ public static void main(String[] args) { URL url; try { url = new URL("http://localhost:8181/sdnext/hello?wsdl"); //1st argument service URI, refer to wsdl document above //2nd argument is service name, refer to wsdl document above QName qname = new QName("http://ws.dineshonjava.com/", "HelloWorldImplService"); Service service = Service.create(url, qname); HelloWorld hello = service.getPort(HelloWorld.class); System.out.println(hello.sayHelloWorld()); } catch (MalformedURLException e) { e.printStackTrace(); } } }
package com.dineshonjava.ws; import javax.jws.WebMethod; import javax.jws.WebResult; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.xml.ws.Action; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.2.2 in JDK 7 * Generated source version: 2.2 * */ @WebService(name = "HelloWorld", targetNamespace = "http://ws.dineshonjava.com/") @SOAPBinding(style = SOAPBinding.Style.RPC) public interface HelloWorld { /** * * @return * returns java.lang.String */ @WebMethod @WebResult(partName = "return") @Action(input = "http://ws.dineshonjava.com/HelloWorld/sayHelloWorldRequest", output = "http://ws.dineshonjava.com/HelloWorld/sayHelloWorldResponse") public String sayHelloWorld(); }2. HelloWorldImplService.java
package com.dineshonjava.ws;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.2 in JDK 7
* Generated source version: 2.2
*
*/
@WebServiceClient(name = "HelloWorldImplService", targetNamespace = "http://ws.dineshonjava.com/", wsdlLocation = "http://localhost:8181/sdnext/hello?wsdl")
public class HelloWorldImplService
extends Service
{
private final static URL HELLOWORLDIMPLSERVICE_WSDL_LOCATION;
private final static WebServiceException HELLOWORLDIMPLSERVICE_EXCEPTION;
private final static QName HELLOWORLDIMPLSERVICE_QNAME = new QName("http://ws.dineshonjava.com/", "HelloWorldImplService");
static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("http://localhost:8181/sdnext/hello?wsdl");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
HELLOWORLDIMPLSERVICE_WSDL_LOCATION = url;
HELLOWORLDIMPLSERVICE_EXCEPTION = e;
}
public HelloWorldImplService() {
super(__getWsdlLocation(), HELLOWORLDIMPLSERVICE_QNAME);
}
public HelloWorldImplService(WebServiceFeature... features) {
super(__getWsdlLocation(), HELLOWORLDIMPLSERVICE_QNAME, features);
}
public HelloWorldImplService(URL wsdlLocation) {
super(wsdlLocation, HELLOWORLDIMPLSERVICE_QNAME);
}
public HelloWorldImplService(URL wsdlLocation, WebServiceFeature... features) {
super(wsdlLocation, HELLOWORLDIMPLSERVICE_QNAME, features);
}
public HelloWorldImplService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public HelloWorldImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* @return
* returns HelloWorld
*/
@WebEndpoint(name = "HelloWorldImplPort")
public HelloWorld getHelloWorldImplPort() {
return super.getPort(new QName("http://ws.dineshonjava.com/", "HelloWorldImplPort"), HelloWorld.class);
}
/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the features
parameter will have their default values.
* @return
* returns HelloWorld
*/
@WebEndpoint(name = "HelloWorldImplPort")
public HelloWorld getHelloWorldImplPort(WebServiceFeature... features) {
return super.getPort(new QName("http://ws.dineshonjava.com/", "HelloWorldImplPort"), HelloWorld.class, features);
}
private static URL __getWsdlLocation() {
if (HELLOWORLDIMPLSERVICE_EXCEPTION!= null) {
throw HELLOWORLDIMPLSERVICE_EXCEPTION;
}
return HELLOWORLDIMPLSERVICE_WSDL_LOCATION;
}
}
Now, create a Java web service client which depends on the above generated files.package com.dineshonjava.client; import com.dineshonjava.ws.HelloWorld; import com.dineshonjava.ws.HelloWorldImplService; /** * @author Dinesh Rajput * */ public class HelloWorldClient { /** * @param args */ public static void main(String[] args) { HelloWorldImplService helloService = new HelloWorldImplService(); HelloWorld hello = helloService.getHelloWorldImplPort(); System.out.println(hello.sayHelloWorld()); } }Here’s the output
Labels: WebService