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. 服务端结构
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最好写到配置文件)。
客户端结构:
附录1.服务端代码
1.
package com.itjob.dao;
public interface GetWeather {
public String get(int day);
}
2.
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;
}
}
3.services.xml文件
<?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>
4.web.xml文件
<?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>
附录2.客服端代码
1.
package com.itjob.dao;
public interface GetWeather {
public String get(int day);
}
2.
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();
}
}
}
运行main。。。