[ajug-members] Webservice with spring IoC

Andrew Goode andrew.goode at discretewireless.com
Wed Mar 9 15:53:45 EST 2011


A problem that immediately jumps out at me is that you have your web service bean defined as a servlet in your web.xml, but it's not an HttpServlet.  You may want to use one of Spring's servlets depending on what type of "web service" we're talking about.

If this is a traditional WSDL-based web service, then take a look at org.springframework.ws.transport.http.MessageDispatcherServlet<http://static.springsource.org/spring-ws/site/apidocs/org/springframework/ws/transport/http/MessageDispatcherServlet.html> in Spring Web Services.  Otherwise, if this is meant to be a RESTful web service, then you'll probably want to use either org.springframework.web.servlet.DispatcherServlet<http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html> or some other FrameworkServlet-based implementation.

I'm no expert on Spring 3 (my project is about to update from Spring 2.5.6.SEC01 to latest Spring 3), but I'm pretty sure it greatly simplifies exposing your Spring beans as RESTful services.  Here's an old blog-post I found in a jiffy (there's probably a lot of newer material out there):
http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/

There may be other problems, but fixing your servlet definition seems like a good first step.  Make sure you read up on the servlet you go with, too.

-Andrew

From: mike barnes [mailto:mdb3624 at gmail.com]
Sent: Wednesday, March 09, 2011 2:45 PM
To: ajug-members at ajug.org
Subject: [ajug-members] Webservice with spring IoC

I am having issues getting spring injection to work with my webservice. Could someone help me understand what I am missing to get spring IoC working with my project. Below is the web.xml,applicationContext.xml and source code.

Thanks in advance:


web.xml
========================================================================
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     version="2.4"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Archetype Created Web Application</display-name>

  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>

    <servlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>com.soltech.webservice.impl.HelloWorldImpl</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>


</web-app>
===============================================================================

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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <bean name="helloWorldService" class="com.soltech.webservice.impl.HelloWorldImpl">
        <property name="serviceName" value="Account Service"/>
    </bean>

</beans>
====================================================================================

HelloWorld.java
====================================================================================
package com.soltech.webservice;

import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(name="HelloWorld")
public interface HelloWorld {

    @WebMethod
    @WebResult
    public String ping();

}
======================================================================================

HelloWorldImpl.java
======================================================================================
package com.soltech.webservice.impl;

import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;

import com.soltech.webservice.HelloWorld;

@WebService(name="HelloWorld")
public class HelloWorldImpl implements HelloWorld {


    private String serviceName;

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    @WebMethod
    @WebResult
    public String ping() {
        System.out.println("ServiceName=["+serviceName+"]");
        return "Service is up";
    }
}
==============================================================================================


When I call ping, my expectation is that I will see on the console "ServiceName=[Account Service]" instead I see "ServiceName=[null]", I also do not see any stack traces or anything else that would lead me to believe that I had missed something.

Again
Any help would be appreciated.

Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.ajug.org/pipermail/ajug-members/attachments/20110309/edbc0fca/attachment.html 


More information about the ajug-members mailing list