Myeclipse 搭建 webservice XFire

Myeclipse6.0 搭建webservice XFire

1. 新建web工程名为wsdemo

2. 右键wsdemo 》 MyEclipse 》Add Web service capabilities 》Next 》
√ MyEclipse Libraries 》
√ Xfire 1.2 Core Libraries
√ Xfire 1.2 HTTP Client Libraries
》Finish

3. 服务端结构
[img]D:\ws1.JPG[/img]

4. 浏览器测试
http://127.0.0.1:6666/wsdemo/services?wsdl

点击wsdl,出现个想xml的东东表示ok。

服务端ok。
--------------------------------------------------------------------
下面是客户端
客户端以String url = "http://127.0.0.1:6666/wsdemo/services/weather";访问(url最好写到配置文件)。
客户端结构:
[img]D:\ws2.JPG[/img]


附录1.服务端代码
1.[code]
package com.itjob.dao;

public interface GetWeather {
public String get(int day);
}
[/code]
2.
[code]
package com.itjob.dao.support;

import com.itjob.dao.GetWeather;

public class GetWeatherSupport implements GetWeather {

public String get(int day) {
// TODO Auto-generated method stub
String result = "";
switch (day) {
case 1: result = "多云";break;
case 2: result = "暴雨";break;
case 3: result = "阴转雨加雪";break;
case 4: result = "阳光灿烂";break;
case 5: result = "零星小雨";break;
case 6: result = "晴转多云";break;
case 7 : result = "大雾";break;
default : result ="天气不太明朗";break;
}
return result;
}
}
[/code]
3.services.xml文件[code]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>weather</name>
<namespace>www.5itjob.com</namespace>
<serviceClass>com.itjob.dao.GetWeather</serviceClass>
<implementationClass>com.itjob.dao.support.GetWeatherSupport</implementationClass>
</service>
</beans>
[/code]
4.web.xml文件[code]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
[/code]
附录2.客服端代码
1.[code]
package com.itjob.dao;

public interface GetWeather {
public String get(int day);
}
[/code]
2.[code]
package com.itjob.app;

import java.net.MalformedURLException;
import org.codehaus.xfire.XFire;
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.itjob.dao.GetWeather;

public class MainApp {

public static void main(String[] args) {
// TODO Auto-generated method stub
Service service = new ObjectServiceFactory().create(GetWeather.class);
XFire xFire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xFire);
String url = "http://127.0.0.1:6666/wsdemo/services/weather";
try {
GetWeather gw = (GetWeather) factory.create(service, url);
System.out.println(gw.get(6));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
[/code]
运行main。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值