您的位置:首页 > 其它

用XFire成功发布WebService

2011-05-26 17:20 253 查看
发布WebService之前必须在项目中添加XFire *.* Core Libraies和XFire *.* HTTP Client Libraries(从MyEclipse中的Java Build Path中获取),下面以HelloWorld为例发布WEB服务。

--------------[b]---------------------------------------source: interface begin------------------------------------------------------[/b]

package com.sun.java;

public interface IHelloWorldService {

public String example1(String message);

}

-----------[b]-----------------------------------------source: interface impl begin--------------------------------------------------[/b]

package com.sun.java;

public class HelloWorldServiceImpl implements IHelloWorldService {

public String example1(String message) {

return message;

}

}

----------------[b]-----------------------------------------配置: web.xml begin------------------------------------------------------[/b]

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>

<servlet-name>XFireServlet</servlet-name>

<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>

<load-on-startup>0</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>XFireServlet</servlet-name>

<url-pattern>/services/*</url-pattern>

</servlet-mapping>

</web-app>

--------------------[b]-------------------------------------配置: services.xml
begin------------------------------------------------[/b]

services.xml文件在Eclipse中的路径为WebContent/WEB-INF/classes/META-INF/xfire/services.xml,在MyEclipse中的路径为WebRoot/WebServices/services.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://xfire.codehaus.org/config/1.0">

<service>

<name>HelloWorldService</name>

<serviceClass>com.sun.java.IHelloWorldService</serviceClass>

<implementationClass>com.sun.java.HelloWorldServiceImpl</implementationClass>

</service>

</beans>

-------------------------[b]-------------------------------客户端: 部署测试 begin---------------------------------------------------

[/b]package com.sun.java.client;

import java.net.MalformedURLException;

import org.codehaus.xfire.XFireFactory;

import org.codehaus.xfire.client.XFireProxyFactory;

import org.codehaus.xfire.service.Service;

import org.codehaus.xfire.service.binding.ObjectServiceFactory;

import com.sun.java.IHelloWorldService;

public class HelloWorldClient {

//方法1

public static void main(String[] args) {

HelloWorldServiceClient client = new HelloWorldServiceClient();

//create a default service endpoint

HelloWorldServicePortType service = client.getHelloWorldServiceHttpPort();

//Add custom client code here

service.example1("hello world");

System.out.println("test client completed");

}

//方法2

public static void main(String[] args) {

Service srvcModel = new ObjectServiceFactory().create(IHelloWorldService.class);

XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());

//发布URL如下,对应的WS描述语言为:http://localhost/HelloWorld/services/HelloWorldService?wsdl

String helloWorldURL = "http://localhost/HelloWorld/services/HelloWorldService";

try {

IHelloWorldService srvc = (IHelloWorldService) factory.create(srvcModel, helloWorldURL);

String result = srvc.example1("hello world");

System.out.print(result);

} catch (MalformedURLException e) {

e.printStackTrace();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: