加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

XFire简单使用

发布时间:2020-12-17 01:25:21 所属栏目:安全 来源:网络整理
导读:1 . XFire可以认为是webservice的一个实现。 其下载地址: http://xfire.codehaus.org/ 下载了以后把lib下的所有jar和 xfire-all-*.jar拷贝到你的项目里。 配置web.xml xml 代码 xml ? version = "1.0" ? encoding = "UTF-8" ? ?? web-app ? id = "WebApp_ID

1 . XFire可以认为是webservice的一个实现。

其下载地址:http://xfire.codehaus.org/

下载了以后把lib下的所有jar和xfire-all-*.jar拷贝到你的项目里。

配置web.xml

xml 代码
  1. xml?version="1.0"?encoding="UTF-8"?>??
  2. <web-app?id="WebApp_ID"?version="2.4"?xmlns="http://java.sun.com/xml/ns/j2ee"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee?http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">??
  3. ??
  4. ?????<servlet>?? ??
  5. ?????????<servlet-name>xfireservlet-name>?? ??
  6. ?????????<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServletservlet-class>?? ??
  7. ?????servlet>?? ??
  8. ??? ??
  9. ?????<servlet-mapping>?? ??
  10. ?????????<servlet-name>xfireservlet-name>?? ??
  11. ?????????<url-pattern>/services/*url-pattern>?? ??
  12. ????servlet-mapping>? ??
  13. web-app>??

?构建Server端

java 代码
  1. package?org.ttitfly; ??
  2. ??
  3. public?class?User?{ ??
  4. ???? ??
  5. ????private?String?username; ??
  6. ????private?int?age; ??
  7. ????public?int?getAge()?{ ??
  8. ????????return?age; ??
  9. ????} ??
  10. ????public?void?setAge(int?age)?{ ??
  11. ????????this.age?=?age; ??
  12. ????} ??
  13. ????public?String?getUsername()?{ ??
  14. ????????return?username; ??
  15. ????} ??
  16. ????public?void?setUsername(String?username)?{ ??
  17. ????????this.username?=?username; ??
  18. ????} ??
  19. ??
  20. }??

?

UserService及UserService的实现类:UserServiceImpl

java 代码
  1. package?org.ttitfly; ??
  2. ??
  3. public?interface?UserService?{ ??
  4. ???? ??
  5. ????public?User?getUserByUsername(String?username); ??
  6. ???? ??
  7. ????public?String?getString(); ??
  8. ??
  9. } ??

?

java 代码
  1. public?class?UserServiceImpl?implements?UserService{ ??
  2. ??
  3. ????private?User?user; ??
  4. ????public?UserServiceImpl(){ ??
  5. ????????user?=?new?User(); ??
  6. ????????user.setUsername("ttitfly"); ??
  7. ????????user.setAge(26); ??
  8. ????} ??
  9. ????public?User?getUserByUsername(String?username){ ??
  10. ????????if("ttitfly".equals(username)){ ??
  11. ????????????return?user; ??
  12. ????????}else{ ??
  13. ????????????return?new?User(); ??
  14. ????????} ??
  15. ????} ??
  16. ????public?String?getString(){ ??
  17. ????????return?"testtest"; ??
  18. ????} ??
  19. } ??

?

在services.xml里配置发布服务的说明。

默认会去classpath下的META-INF/xfire/services.xml找这个文件。

xml 代码
  1. <beans?xmlns="http://xfire.codehaus.org/config/1.0">?? ??
  2. ?????<service>?? ??
  3. ?????????<name>UserServicename>?? ??
  4. ?????????<namespace>sdfjslfjslfkjlsnamespace>?? ??
  5. ?????????<serviceClass>org.ttitfly.UserServiceserviceClass>?? ??
  6. ?????????<implementationClass>org.ttitfly.UserServiceImplimplementationClass>?? ??
  7. ?????service>?? ??
  8. beans>????

?

客户端代码:

java 代码
  1. package?org.ttitfly; ??
  2. ??
  3. import?java.net.MalformedURLException; ??
  4. ??
  5. import?org.apache.commons.logging.Log; ??
  6. import?org.apache.commons.logging.LogFactory; ??
  7. import?org.codehaus.xfire.client.XFireProxyFactory; ??
  8. import?org.codehaus.xfire.service.Service; ??
  9. import?org.codehaus.xfire.service.binding.ObjectServiceFactory; ??
  10. import?org.springframework.util.Assert; ??
  11. ??
  12. ??
  13. @SuppressWarnings("unchecked") ??
  14. public?class?XFireClientFactory?{ ??
  15. ????private?static?XFireProxyFactory?serviceFactory?=?new?XFireProxyFactory(); ??
  16. ??
  17. ????private?static?final?Log?log?=?LogFactory.getLog(XFireClientFactory.class); ??
  18. ??
  19. ????private?XFireClientFactory()?{ ??
  20. ????} ??
  21. ??
  22. ????/** ?
  23. ?????*?获得POJO形式的Web服务的客户端Proxy类. ?
  24. ?????* ?
  25. ?????*?@param?serviceURL???Web?ServiceURL ?
  26. ?????*?@param?serviceClass?Web?Service接口类 ?
  27. ?????*?@return?类型为Web?Service接口的客户端Proxy类 ?
  28. ?????*/??
  29. ????public?static?<t></t>?T?getClient(String?serviceURL,?Class<t></t>?serviceClass)?{ ??
  30. ????????Assert.notNull(serviceURL); ??
  31. ????????Assert.notNull(serviceClass); ??
  32. ????????Service?serviceModel?=?new?ObjectServiceFactory().create(serviceClass); ??
  33. ????????try?{ ??
  34. ????????????return?(T)?serviceFactory.create(serviceModel,?serviceURL); ??
  35. ????????}?catch?(MalformedURLException?e)?{ ??
  36. ????????????log.error(e.getMessage(),?e); ??
  37. ????????????e.printStackTrace(); ??
  38. ????????????return?null; ??
  39. ????????} ??
  40. ????} ??
  41. ??
  42. ??
  43. } ??

?

客户端调用类:

java 代码
  1. package?org.ttitfly; ??
  2. ??
  3. public?class?TestClient?{ ??
  4. ??
  5. ????public?static?void?main(String[]?args)?{?? ??
  6. ?????????????????????????????? ??
  7. ?????????String?serviceURL?=?"http://localhost:8080/xfireTest/services/UserService";?? ??
  8. ?????????try?{?? ??
  9. ?????????????UserService?service?=?XFireClientFactory.getClient(serviceURL,?UserService.class);?? ??
  10. ?????????????System.out.println(service.getUserByUsername("ttitfly").getAge()); ??
  11. ?????????????System.out.println(service.getString());?? ??
  12. ?????????}?catch?(Exception?e)?{?? ??
  13. ?????????????e.printStackTrace();?? ??
  14. ?????????}?? ??
  15. ??? ??
  16. ?????}? ??
  17. } ??

?

启动tomcat:输入:http://localhost:8080/xfireTest/services/UserService?wsdl?可以查看生成的wsdl文档

如果输入:http://localhost:8080/xfireTest/services/UserService?输出的是:Invalid SOAP request. 也是正常的

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读