ofbiz的webservice接口提供(1)-WSDL的生成

本文深入探讨了OFBiz框架中服务模块的工作原理及其如何轻松地将服务暴露为Web服务。通过设置服务文件中的特定属性,实现将内部实体对象的CRUD操作转化为Web服务接口,便于跨系统间进行业务交互。文章详细解析了服务定义文件和服务类的构建,展示了如何利用控制器文件支持Web服务的外部访问,并提供了关键步骤和实例代码。此外,还介绍了OFBiz项目中所有暴露给外部访问的Web服务的查看方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ofbiz的service:这个是ofbiz赖以骄傲的设计方式。她可以将所有内部实体对象的CRUD都使用service的方式提供,不同系统之间可以通过互相调用service来完成业务操作。这种松耦合的方式是很多框架梦寐以求的效果。

在ofbiz4的文档中提到,如果想将ofbiz的某个服务开放成webservice只是需要轻轻的将我们定义service文件中的service属性中的export设定为true。

例如:我的一个ofbiz项目的servicedef目录下的services.xml文件中定义了一个服务

[xhtml] view plain copy
  1. <service
  2. name="findSeniorService"
  3. engine="java"
  4. location="com.hc360.cem.ws.CEMSeniorMemberService"
  5. invoke="findSeniorService"
  6. export="true"
  7. validate="false"
  8. auth="false">
  9. <description>CRM call CEM findSeniorService soap</description>
  10. <attribute name="userid" type="String" mode="INOUT" optional="true"/>
  11. <attribute name="salt" type="String" mode="IN" optional="true"/>
  12. <attribute name="aaa" type="String" mode="OUT" optional="true"/>
  13. <attribute name="bbb" type="String" mode="OUT" optional="true"/>
  14. <attribute name="ccc" type="String" mode="OUT" optional="true"/>
  15. </service>

上边图片的意思就是将:com.hc360.cem.ws.CEMSeniorMemberService类中的findSeniorService作为soap接口提供出去。输入参数有userid、salt,输出参数有userid、aaa、bbb、ccc。

而我实际类如下:

[c-sharp] view plain copy
  1. import java.util.Map;
  2. import javolution.util.FastMap;
  3. import org.ofbiz.base.util.Debug;
  4. import org.ofbiz.service.DispatchContext;
  5. public class CEMSeniorMemberService {
  6. /**
  7. * 开放一个service供测试使用
  8. * wsdl的访问方式:http://yourip:port/project/control/SOAPService/findSeniorService?wsdl
  9. * 但是ofbiz给我们生成的wsdl使用任何的客户端生成工具都无法正确生成,但是这个webservice接口是可以使用的
  10. * @author kongqz
  11. * @date 2009-03-11
  12. *
  13. * */
  14. public static Map<String, Object> findSeniorService(DispatchContext ctx, Map<String, ? extends Object> context) {
  15. //存放结果的map
  16. Map<String, Object> result = FastMap.newInstance();
  17. // GenericDelegator delegator = ctx.getDelegator();
  18. String userid = (String) context.get("userid");
  19. String salt = (String) context.get("salt");
  20. Debug.logInfo("salt is ["+salt+"],userid is ["+userid+"] ", "findSeniorService");
  21. result.put("aaa", "test_aaaaa");
  22. result.put("bbb", "test_bbbbb");
  23. result.put("ccc", "test_ccccc");
  24. result.put("userid", userid);
  25. return result;
  26. }
  27. }

我传入的参数将从ofbiz的这个context中获取,而我返回的参数将通过result这个map来put出去。

这里我想说明的是:这个时候如果我调用如下链接:

http://yourip:port/projectname/control/SOAPService/findSeniorService?wsdl

就可以看到我这个服务的wsdl文件。

如果想看整个项目所有暴露给外部访问的webservice有哪些,我可以使用

http://yourip:port/projectname/control/SOAPService?wsdl

来进行查看。

通过上边wsdl链接我们需要知道一点,我们的SOAPService哪里来的?

这里就需要指出,如果想将你的SOAPService暴露给外部,需要ofbiz的controller来做点贡献。因为ofbiz的外部请求都是通过ofbiz的servlet来处理的,入口点是项目的controller文件,我们需要给controller文件增加支持。

controller文件:ofbiz当前项目的所有请求的入口,通过对应request-map:将所有的请求uri对应到指定的处理函数上。

增加如下:

[xhtml] view plain copy
  1. <!-- 引擎接口 -->
  2. <request-map uri="httpService">
  3. <event type="java" path="org.ofbiz.service.engine.HttpEngine" invoke="httpEngine"/>
  4. <response name="success" type="none"/>
  5. <response name="error" type="none"/>
  6. </request-map>
  7. <request-map uri="SOAPService">
  8. <event type="soap"/>
  9. <response name="error" type="none"/>
  10. <response name="success" type="none"/>
  11. </request-map>
  12. <request-map uri="xmlrpc" track-serverhit="false" track-visit="false">
  13. <event type="xmlrpc"/>
  14. <response name="error" type="none"/>
  15. <response name="success" type="none"/>
  16. </request-map>

通过在controller.xml文件上的支持,你才能将httpService,SOAPService,xmlrpc这些服务对外提供,你的链接才能写成上边的方式。

总结下ofbiz的webservice提供前提:

1、controller的支持,SOAPService的接口暴露】

2、service类的提供,这个类是static方式的,数据的传入传出使用map方式(注意那个context)

3、services.xml文件的定义,将你的webservice定义在这里,并设定export=true,否则只能是一个内部的service了。

4、访问我们项目提供的所有webservice,看wsdl文件是否可用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值