Jun 29 2008

Contract First Web Services with Spring 2 and JAX-WS

Published by at 10:15 PM under Java,NetBeans,Spring,web services



This short blog entry will show you how easily you can integrate JAX-WS into a Spring 2 Project. The example sourcecode is depending on my previous blog How to modify JAX-WS SOAP-Messages and i am using NetBeans 6.1.


Prerequisites

Download the JAX-WS commons and XBean.


Transform the SOAP Web Services Project into a Spring Project

First open the Non-Spring-Project “How to modify JAX-WS SOAP-Messages“, switch to the project-properties and add the XBean, JAX-WS commons and Spring Framework 2.5 JAR-files.

add jar files to netbeans project


You can see in this picture which JAR-files have to be included in the project:

jar files in the netbeans project


Now you have to create the Spring Configuration file. Create the new XML-file: /WEB-INF/applicationContext.xml. If you are using NetBeans 6.1, navigate to File->New File->Spring Framework->Spring XML Configuration File.

create a new spring configuration file in netbeans 6.1


Replace the content with this:

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ws="http://jax-ws.dev.java.net/spring/core"
xmlns:wss="http://jax-ws.dev.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.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd">
    <ws:service id="SOAPservice" impl="eu.jdevelop.soapwebservices.service.ServiceImpl">
        <ws:handlers>
            <ref bean="myHandler" />
        </ws:handlers>
    </ws:service>

    <wss:bindings id="jax-ws.http">
        <wss:bindings>
            <wss:binding url="/soapwebservices" service="#SOAPservice"/>
        </wss:bindings>
    </wss:bindings>

    <bean id="myHandler" class="eu.jdevelop.soapwebservices.handler.ServerSOAPHandler" />
</beans>


applicationContext.xml


Modify the web.xml to load the Spring Configuration file.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>SpringWeb</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>jaxws-servlet</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>jaxws-servlet</servlet-name>
        <url-pattern>/soapwebservices</url-pattern>
    </servlet-mapping>
</web-app>



Open the file “ServiceImpl.java” and remove this line:

@HandlerChain(name = "ServerSOAPHandler", file = "serversoaphandler.xml")


You don’t need this annotation anymore, Spring is injecting the “ServerSOAPHandler.java” into the ServiceImpl.java.

the modified ServiceImpl.java

This application is now Spring-enabled. Enjoy the benefits of JAX-WS with Spring 2.

Technorati Tags: , , , , , ,

4 responses so far

4 Responses to “Contract First Web Services with Spring 2 and JAX-WS”

  1. [...] can read the rest of this blog post by going to the original source, here [...]

  2. George sagt:

    Hi dude.
    Nice post. But do you have any idea on how to integrate the JAX-WS REST web services with spring.

    Thanks in advance

  3. Andrew sagt:

    Hello Siegfried,

    Thanks for the example that I found somewhat better than the one supplied at https://jax-ws-commons.dev.java.net/spring.

    Just one remark when using the code: specifying the wsdlLocation attribute in the @WebService annotation of ServiceImpl.java causes an RTE:

    SEVERE: Exception occured in J2EEC Phasejava.lang.RuntimeException: wsdl file WEB-INF/wsdl/ServiceImpl/webservices.wsdl does not exist for web service SOAPService
    com.sun.enterprise.deployment.backend.IASDeploymentException: Error loading deployment descriptors for module [spring-jaxws] — wsdl file WEB-INF/wsdl/ServiceImpl/webservices.wsdl does not exist for web service SOAPService

    It should be removed. Any idea why this occurs ?

    Thanks

  4. Shameer sagt:

    Sharing a similar post with title, Building a web service with Spring-WS at http://justcompiled.blogspot.com/2010/09/building-web-service-with-spring-ws.html