Tuesday, August 12, 2008

Writing your first Spring web service in WSO2 WSF/Spring

WSO2 WSF/Spring is a project build on top of Apache Axis2 to provide an easy way to way to make Spring beans into Web services. I blogged about a WSF/Spring demo app in my last post and now I am going to use that demo app to expose a Spring bean as a web service using the code first approach.We will also invoke the service and make sure that it is working as expected. Good news is to try out all these, it won't take more than 10 minutes.

We are going to create StockQuote web service which will return quotes for given symbol. We will be using the WSO2 WSF/Spring demo application for this. Source code for this post can be found here. If you have not already read “WSO2 WSF/Spring - Apache Axis2 for spring web services developers”, please have a look at it first.

Oky now, let's look at the bean that we want to expose as a web service.

package org.wso2.training.wsfspring;

import java.util.Map;

public class StockQuoteService {

private Map quotesMap;

public Map getQuotesMap() {
return quotesMap;
}

public void setQuotesMap(Map quotesMap) {
this.quotesMap = quotesMap;
}

public String getQuote(String company) throws Exception {
if (quotesMap.containsKey(company)) {
return quotesMap.get(company);
} else {
throw new Exception("Ooops, " + company + " is not listed with us");
}
}
}

So we have to put this java source file in the source directory [wsfspring/src/main/java/] under the correct package structure.

Now let's look at the applicationContext.xml [src/main/webapp/WEB-INF/applicationContext.xml] where all the Spring configuration is done. First we define the "StockQuote" bean.

<bean id="StockQuote" class="org.wso2.training.wsfspring.StockQuoteService">
<property name="quotesMap">
<map>
<entry key="wso2" value="150"/>
<entry key="ibm" value="100"/>
<entry key="microsoft" value="100"/>
</map>
</property>

</bean>

Now we have defined the bean and we need expose it as a web service. We use “services” bean for that. Using the “serviceBean” property we refer to actual bean we want to expose as web service and "serviceName" defines the name of the service as given below.

<bean id="services" class="org.wso2.spring.ws.WebServices">
<property name="services">
<list>
<bean id="StockQuoteService" class="org.wso2.spring.ws.SpringWebService">
<property name="serviceBean" ref="StockQuote"/>
<property name="serviceName" value="StockQuoteService"/>
</bean>
</list>
</property>
</bean>


And that is all you need to do to expose your Spring bean as a web service.

Oky, let's try this out. As described in the previous post all you got to do is run the maven build.
~../wsfspring$ mvn jetty:run
Now if you go to
http://localhost:8080/wsfspring/
you will be able to find StockQuoteService under services/available services.
So how can we invoke this service. Axis2 allows you to invoke the services through the web browser in a RESTful manner. So if we invoke StockQuoteService through the browser using
http://localhost:8080/wsfspring/services/StockQuoteService/getQuote?company=wso2
we will be able to see








and if we try a company that is not listed using
 http://localhost:8080/wsfspring/services/StockQuoteService/getQuote?company=oracle









So that's it. As you can see, it is very easy to create web services with WSO2 WSF/Spring. Next we will be look at how to secure WSF/Spring web services with Apache Rampart.Apache Rampart integration is a great advantage in WSF/Spring as Apache Rampart already supports most of the WS - Sec* specifications including
  • WS - Security 1.0 / 1.1
  • WS - Security Policy 1.1 / 1.3
  • WS - Secure Conversation 2005 / WS-SX
  • WS - Trust 2005 / WS-SX
specifications. So you will be able to get the full benefit of WS - Security using WSO2 WSF/Spring.

1 comments:

Javier said...

Please... Can somebody help mi creating a web service server with security?? how can I use polycies.xml??