Thursday, April 24, 2008

Web Service with Spring Configuration

     I'm a complete newbie when it comes to Web Service. So when someone at work asked me to take a look at a new Web Service project and figure out why the spring configuration isn't working, my mind just went blank.

     Anyway, I did what he asked. First of all, all beans are correctly defined in the configuration file. Second, all setters and getters are promptly defined as well. Third, he figured out himself to extend spring's ServletEndpointSupport class. Then, here comes the problem. All the beans are still not wired when calling the service, so it keeps throwing NullPointerException.

     It turns out that even if you extended ServletEndpointSupport, you still need to manually wire the beans yourself. But in that case, you only need to wire them once, not in every service call.

     Here is what's missing in my co-worker's code:

/* (non-Javadoc)
 * @see org.springframework.remoting.jaxrpc.ServletEndpointSupport#onInit()
 */
@Override
protected void onInit() throws javax.xml.rpc.ServiceException {
    [bean name]=([bean type class]) this.getWebApplicationContext().
        getBean([bean id in configuration]);
}


     Oh, by the way, you can remove those setters and getters now if you like.