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.
So we have to put this java source file in the source directory [wsfspring/src/main/java/] under the correct package structure.
package org.wso2.training.wsfspring;
import java.util.Map;
public class StockQuoteService {
private MapquotesMap;
public MapgetQuotesMap() {
return quotesMap;
}
public void setQuotesMap(MapquotesMap) {
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");
}
}
}
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.
And that is all you need to do to expose your Spring bean as a web service.
<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>
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:runNow 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=wso2we 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












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