Monday, September 15, 2008

Exposing JAX-WS web service using Spring

We start by creating a web project in Eclipse. I used Sysdeo Tomcat plugin.This will create a basic web application structure. Note that this does not create a web.xml file in the WEB-INF folder.
Add the following jar files to the WEB-INF\lib folder

activation.jar
commons-collections.jar
commons-logging.jar
FastInfoset.jar
http.jar
jaxb-api.jar
jaxb-impl.jar
jaxb-xjc.jar
jaxws-api.jar
jaxws-rt.jar
jaxws-spring-1.7.jar
jsr173_api.jar
jsr181-api.jar
jsr250-api.jar
resolver.jar
saaj-api.jar
saaj-impl.jar
sjsxp.jar
spring.jar
stax-ex.jar
streambuffer.jar
xbean-spring-2.8.jar

You are required to download the jaxws-spring-1.X.jar from https://maven2-repository.dev.java.net/source/browse/*checkout*/maven2- repository/trunk/www/repository/org/jvnet/jax-ws-commons/spring/jaxws-spring/1.8/jaxws-spring-1.8.jar?rev=3913 (This is the latest available version. I used 1.7 version.)
If you are using Java SE 6 some of the jars may not be required. Also if you need to use the latest version of JAX-WS with Java SE 6 you will need to use the endorsed mechanism for loading the JAX-WS libraries.I am using Java SE 5. Here is the simple web service class:

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;

/**
* Please run the wsgen if you are using Style.DOCUMENT
* Style.RPC used only for demo purpose.
* @author
*
*/
@WebService
@SOAPBinding(style = Style.RPC, use = Use.LITERAL)
public class HelloService {

   @WebMethod
   public String sayHello(@WebParam(name="name")String name) {
       return "Hello " + name;
   }

}
Create an applicationContext.xml file in the WEB-INF folder as shown.


   
   

   

Create a web.xml file in the WEB-INF folder and add the following entries to it.

    contextConfigLocation
    /WEB-INF/applicationContext.xml




   
       org.springframework.web.context.ContextLoaderListener
   




   jaxws-servlet
   
      com.sun.xml.ws.transport.http.servlet.WSSpringServlet
   



   jaxws-servlet
   /hello



This service is configured using the <wss:binding> tag in the applicationContext.xml file as described above. <wss:binding> definitions define what services are exposed to which part of the URL space. You can also use the nested syntax for the configuration as shown below:
 
    
        
    

Server side Handlers for the service can also be configured using the <ws:service> definition as shown below:




If the service uses more than one handler, handlers can be nested.

   
       
       
   

Deploy the web project on tomcat (tested on Tomcat 6.0). The service WSDL can be accessed using the following URL http://<your-ip>:<tomcat-port>/<project-name>/hello?wsdl
Also I would suggest to have a look at Apache CXF.

8 comments:

  1. Good Article....

    You can find
    Simple JaxWs webservice woth jobss sample here http://velmurugan-pousel.blogspot.com

    ReplyDelete
  2. Great article. If you are using Maven 2 you can use following dependencies and repos:



    org.springframework
    spring
    2.5.6


    org.jvnet.jax-ws-commons.spring
    jaxws-spring
    1.8


    com.sun.xml.bind
    jaxb-impl
    2.2





    maven-repository.java.net
    Java.net Repository for Maven 1
    http://download.java.net/maven/1/
    legacy


    maven2-repository.java.net
    Java.net Repository for Maven 2
    http://download.java.net/maven/2/

    ReplyDelete
  3. I can see here some way to expose web services using spring:
    http://static.springsource.org/spring-ws/sites/2.0/reference/html/server.html

    which is the more flexible way to achieve exposing a web service? jaxws-spring or the one mentioned in that article? by the way, more complex.

    ReplyDelete
  4. Developing Web Services using jax-ws, spring and maven

    http://j2eeroad.wordpress.com/2011/06/15/developing-web-services-using-jax-ws-spring-and-maven-3/

    ReplyDelete
  5. Fantastic! The firewall at work blocks access to this site - if they hadn't I wouldn't have wasted so much time trying to get things working!!!

    ReplyDelete
  6. I'm getting below exception using JAX-WS with spring 2.0 (jaxws-spring-1.8.jar)

    org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://jax-ws.java.net/spring/servlet.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .

    I found that few URLs in the applicationcontext xml is broken.
    -----------------



    what else i need to use.. Please suggest me on this.. awaiting reply..

    Thanks.

    ReplyDelete
  7. I'm getting below exception using JAX-WS with spring 2.0 (jaxws-spring-1.8.jar)

    org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://jax-ws.java.net/spring/servlet.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

    I found that few URLs in the applicationcontext xml is broken.
    -----------------
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ws="http://jax-ws.java.net/spring/core"
    xmlns:wss="http://jax-ws.java.net/spring/servlet"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://jax-ws.java.net/spring/core
    http://jax-ws.java.net/spring/core.xsd
    http://jax-ws.java.net/spring/servlet
    http://jax-ws.java.net/spring/servlet.xsd">


    what else i need to use.. Please suggest me on this.. awaiting reply..

    Thanks.

    ReplyDelete