Posted on

Advanced JAX-WS Web services sample chapter

 

Coding a Web Service using JAX-WS is a portable choice that will let you run your Web Service on most Java containers (such as Tomcat or Glassfish). On the other hand, when porting your Web Service application to Servlet container such as Tomcat, you need a different procedure for building and packaging your application; actually, Apache Tomcat does not ship with a JAX-WS implementation, therefore the following steps are required in order to deploy your Web Service.

At first you need to pack along with your application a valid JAX-WS  implementation. The most straightforward solution is to rely on the JAX-WS Reference Implementation (RI), which is available at: https://jax-ws.java.net/ . The libraries need to be bundled in the WEB-INF/lib folder of your application deployment; when using Maven for building the application, that is usually simply achieved by declaring the following dependency in the project’s pom.xml:

<dependencies>          <dependency>              <groupId>com.sun.xml.ws</groupId>              <artifactId>jaxws-rt</artifactId>              <version>2.1.3</version>          </dependency>  </dependencies>

Next, in the web.xml descriptor of your application, you need to declare a ServletListener class, which parses the Sun’s RI configuration file,sun-jaxws.xml.

<web-app>         <listener>               <listener-class>                      com.sun.xml.ws.transport.http.servlet.WSServletContextListener               </listener-class>         </listener>         <servlet>               <servlet-name>echo</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>echo</servlet-name>               <url-pattern>/echo</url-pattern>         </servlet-mapping>  </web-app>

Finally, a sun-jaxws.xml file has to be included within the WEB-INF folder of your Web application:

<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"         version="2.0">         <endpoint name="HelloWorld"               implementation="com.itbuzzpress.chapter1.HelloWorld"               url-pattern="/echo" />  </endpoints>

The above file defines the Service Endpoint Implementation class as the endpoint of your project, along with the URL pattern of the Web Service.

Once the application has been packaged, it can be deployed to the Web container using the available management instruments. On Tomcat, you can simply drop the Web application in the webapps folder to start the deployment procedure.

When deploying the Web Service to Apache Tomcat you need to specify the SOAP Binding to be used (DOCUMENT or RPC) on the Web Service, as indicated in the following snippet:

@WebService  @SOAPBinding(style = Style.RPC)    public class HelloWorld   {  . . . .  }

Actually, using the @SOAPBinding annotation is optional, because if not specified it defaults to the DOCUMENT. However, the Tomcat deployer will issue an error if you fail to specify the SOAPBinding.

The main difference between DOCUMENT and RPC is that a DOCUMENT invocation can be fully validated by a standard validating XML parser, while an RPC invocation cannot because of the implied wrapper element around the invocation body.