如果你的项目需要用到webservice接口,那么这篇文章就是个非常好的例子:
1、webservice分为服务端、客户端。
2、服务端接口:
web.xml文件中添加
<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-INF/classes目录下面创建 META-INF/xfire,并在该目录下放置services.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<service xmlns="http://xfire.codehaus.org/config/1.0">
<name>xfireService</name>
<serviceClass>com.vpn.interfacepkg.xfire.interf.IReceiverUserService</serviceClass>
<implementationClass>com.vpn.interfacepkg.xfire.server.ReceiverUserServiceImpl</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
</beans>
注意: 当xmlns="http://xfire.codehaus.org/config/1.0"放到beans后面的时候报错,那么就是xfire-spring版本问题。需要把xmlns放到 services 里面。
最后就是编写类与接口
com.vpn.interfacepkg.xfire.interf.IReceiverUserService
com.vpn.interfacepkg.xfire.server.ReceiverUserServiceImpl
到此,你的服务端的webservice就写好了,重启Tomcat就能启动。
3、客户端调用
代码样例如下
public static void main(String[] args) {
try {
Client client = new Client(new URL("http://localhost:8080/fjzwvpdn/services/xfireService?wsdl"));
FjzwVpnUserInfo userInfo =new FjzwVpnUserInfo("huangyunxbbbb@163.com","123","","","","","","","","","","");
List v =new ArrayList();
v.add(userInfo);
Object[] s = client.invoke("setUserInfo", new Object[]{FjzwVpnUserInfo.listToXML(v)});
System.out.print(s[0].toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
评论