package com.dineshonjava.ws; import javax.jws.WebMethod; import javax.jws.WebService; /** * @author Dinesh Rajput * */ @WebService public interface WSServiceHello { @WebMethod String sayHello(String name); }
package com.dineshonjava.ws; import javax.jws.WebService; /** * @author Dinesh Rajput * */ @WebService(endpointInterface="com.dineshonjava.ws.WSServiceHello") public class WSServiceHelloImpl implements WSServiceHello { @Override public String sayHello(String name) { return "Hello, Welcome to JAX-WS World and keep learning!! " + name; } }
package com.dineshonjava.publisher; import javax.xml.ws.Endpoint; import com.dineshonjava.ws.WSServiceHelloImpl; /** * @author Dinesh Rajput * */ public class WSHelloPublisher { public static void main(String[] args) { Endpoint.publish("http://localhost:8181/WS/WSServiceHello" ,new WSServiceHelloImpl()); } }5) Run above program.Your web service is published.You can check your service wsdl at http://localhost:8181/WS/WSServiceHello?wsdl
<?xml version="1.0" encoding="UTF-8"?> <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.dineshonjava.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.dineshonjava.com/" name="WSServiceHelloImplService"> <types> <xsd:schema> <xsd:import namespace="http://ws.dineshonjava.com/" schemaLocation="http://localhost:8181/WS/WSServiceHello?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="WSServiceHello"> <operation name="sayHello"> <input message="tns:sayHello"/> <output message="tns:sayHelloResponse"/> </operation> </portType> <binding name="WSServiceHelloImplPortBinding" type="tns:WSServiceHello"> <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="WSServiceHelloImplService"> <port name="WSServiceHelloImplPort" binding="tns:WSServiceHelloImplPortBinding"> <soap:address location="http://localhost:8181/WS/WSServiceHello"/> </port> </service> </definitions>
CD %CLIENT_PROJECT_HOME%\src wsimport -keep http://localhost:8181/WS/WSServiceHello?wsdl
package com.dineshonjava.client; import com.dineshonjava.ws.WSServiceHello; import com.dineshonjava.ws.WSServiceHelloImplService; /** * @author Dinesh Rajput * */ public class WSHelloClient { public static void main(String[] args) { WSServiceHelloImplService service = new WSServiceHelloImplService(); WSServiceHello serviceHello = service.getWSServiceHelloImplPort(); System.out.println("***************Call Started*******************"); System.out.println(serviceHello.sayHello("Dinesh")); System.out.println("***************Call Ended*********************"); } }4) Run above program and you will get following output.
Labels: WebService