面向服务架构(SOA,Services-oriented architecture)和Web Services已经变成了当前行业的热点话题。XFire正是在这种情况下产生的。它能够使POJO发布为最少的Web Services,而只需要付出最小化的努力。从而,它使希望使用这项技术的初级开发者的学习曲线变得平缓。同时,由于它兼容最新标准和提供了丰富的API,XFire为高级用户提供了更多的大好机会。
步骤清单:
1、 检查Java类的方法和默认构造函数确保为public
2、 增加XFire servlet相关条目到web.xml中
3、 创建services.xml,把它放到WEB-INF/classes/META-INF/xfire目录下
4、 增加XFire和第三方包到你的Web应用的WEB-INF/lib文件夹中
实际例子:
0. 定义 web.xml
<servlet>
<servlet-name>XFireServlet</servlet-name>
<display-name>XFire Servlet</display-name>
<servlet-class>org.codehaus.xfire.transport.http.XfireConfigurableServlet
</servlet-class>
</servlet><servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
1. 定义接口
public interface IBankingService {
public String transferFunds(
String fromAccount, String toAccount, double amount, String currency);
}
2.定义实现
import java.text.NumberFormat;
import java.text.DecimalFormat;
/** XFire WebServices sample implementation class.
*/
public class BankingService implements IBankingService {
//Default constructor.
public BankingService(){
}
/** Transfers fund from one account to another.
*/
public String transferFunds(
String fromAccount, String toAccount, double amount, String currency){
String statusMessage = "";
//Call business objects and other components to get the job done.
//Then create a status message and return.
try {
NumberFormat formatter = new DecimalFormat("###,###,###,###.00");
statusMessage = "COMPLETED: " + currency + " " + formatter.format(amount)+
" was successfully transferred from A/C# " + fromAccount + " to A/C# " + toAccount;
} catch (Exception e){
statusMessage = "BankingService.transferFunds(): EXCEPTION: " + e.toString();
}
return statusMessage;
}
}
3. 定义 web 描述
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>Banking</name>
<namespace>mybank</namespace>
<serviceClass>com.mybank.xfire.example.IBankingService</serviceClass>
<implementationClass>com.mybank.xfire.example.BankingService</implementationClass>
</service>
</beans>
4.部署获得WSDL
WSDL的URL应该是:http://localhost:8080/websvc/services/Banking?wsdl。
5. 开发客户端
//Create a metadata of the service
Service serviceModel = new ObjectServiceFactory().create(IBankingService.class);
log.debug("callSoapServiceLocal(): got service model." );
//Create a proxy for the deployed service
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
String serviceUrl = "http://localhost:8080/websvc/services/Banking";
IBankingService client = null;
try {
client = (IBankingService) factory.create(serviceModel, serviceUrl);
} catch (MalformedURLException e) {
log.error("WsClient.callWebService(): EXCEPTION: " + e.toString());
}
//Invoke the service
String serviceResponse = "";
try {
serviceResponse = client.transferFunds(fromAccount, toAccount, amount, currency);
} catch (Exception e){
log.error("WsClient.callWebService(): EXCEPTION: " + e.toString());
serviceResponse = e.toString();
}