JAX-WS is an API for building web services and clients. It is the next generation of JAX-RPC.
Building web services with JAX-WS is pretty straight forward though it might take some time to know things better. Here are the steps for building Java web service, which I developed and tested with JAX-WS 2.1.2.1 , Java 1.5 , WebLogic 9.2 and Tomcat 6.0.
Step #1 - Write an interface
package com.darla.ws;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService(targetNamespace = "http://darla.in", name = "HelloWorld")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL)
public interface HelloWorld
{
String sayHello(@WebParam(name = "name") String name);
}
Step #2 - Implementing the interface.
package com.darla.ws;
import javax.jws.WebService;
@WebService(endpointInterface = "com.darla.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld
{
public String sayHello(String name)
{
System.out.println("Text entered : " + name);
return " Hello " + name;
}
}
Step #3 - 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='helloService'
implementation='com.darla.ws.HelloWorldImpl'
url-pattern='/helloService' />
</endpoints>
Step #4 - web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<description>JAX-WS endpoint</description>
<display-name>WSServlet</display-name>
<servlet-name>WSServlet</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>WSServlet</servlet-name>
<url-pattern>/helloService</url-pattern>
</servlet-mapping>
</web-app>
Step #5 - weblogic.xml ( for only WebLogic , not required for Tomcat )
<!DOCTYPE weblogic-web-app PUBLIC "-//BEA Systems, Inc.//DTD Web Application 8.1//EN"
"http://www.bea.com/servers/wls810/dtd/weblogic810-web-jar.dtd">
<weblogic-web-app>
<container-descriptor>
<prefer-web-inf-classes>true</prefer-web-inf-classes>
</container-descriptor>
</weblogic-web-app>
Step #6 - Build and deloy
The different Ant tasks used in build and deployment of web services.
Apt - JDK annotation processing tool, or Apt - generates the request and response classes.
wsgen - Generates wsdl and xsd files.
wsimport - Generates the files that are needed for the client to call/invoke the web service, dont forget to include jaxb binding file for xml binding.
Step #7 - Generating the client
for weblogic - enter the appropriate url and port - by default 7001.
for tomcat - enter the appropriate url and port - by default 8080.
Step #7 - Invoking the service
Here is the sample code to invoke the service.
package com.darla.ws.client;
import com.darla.ws.HelloWorldImplService;
import com.darla.ws.HelloWorld;
public class HelloWorldClient
{
public static void main(String[] argv)
{
HelloWorld helloService = new HelloWorldImplService().getHelloWorldServiceImplPort();
//invoke business method
System.out.println(helloService.sayHello("Manohar"));
}
}
Thats it, Enjoy developing web services...
Having a masters degree in Computational Science, working in Java, developed applications on different domains such as Finanical Services, B2B, Banking, Utility...