ofbiz的service:这个是ofbiz赖以骄傲的设计方式。她可以将所有内部实体对象的CRUD都使用service的方式提供,不同系统之间可以通过互相调用service来完成业务操作。这种松耦合的方式是很多框架梦寐以求的效果。
在ofbiz4的文档中提到,如果想将ofbiz的某个服务开放成webservice只是需要轻轻的将我们定义service文件中的service属性中的export设定为true。
例如:我的一个ofbiz项目的servicedef目录下的services.xml文件中定义了一个服务
<service name="findSeniorService" engine="java" location="org.ofbiz.hello.RGDMemeberServices" invoke="findSeniorService" export="true" validate="false" auth="false"> <description>CRM call CEM findSeniorService soap</description> <attribute name="userid" type="String" mode="INOUT" optional="true"/> <attribute name="salt" type="String" mode="IN" optional="true"/> <attribute name="aaa" type="String" mode="OUT" optional="true"/> <attribute name="bbb" type="String" mode="OUT" optional="true"/> <attribute name="ccc" type="String" mode="OUT" optional="true"/> </service>
上边图片的意思就是将:org.ofbiz.hello.RGDMemeberServices类中的findSeniorService作为soap接口提供出去。输入参数有userid、salt,输出参数有userid、aaa、bbb、ccc。
而我实际类如下:
package org.ofbiz.hello;
import java.util.Map;
import javolution.util.FastMap;
import org.ofbiz.base.util.Debug;
import org.ofbiz.service.dispatchContext;
// uses Log4J
// helpful utility for working with Maps,Lists,etc.
public class RGDMemeberServices {
public static final String module = RGDMemeberServices.class.getName(); // used for debugging
/**
* 开放一个service供测试使用
* wsdl的访问方式:http://yourip:port/project/control/SOAPService/findSeniorService?wsdl
* 但是ofbiz给我们生成的wsdl使用任何的客户端生成工具都无法正确生成,但是这个webservice接口是可以使用的
* @author kongqz
* @date 2009-03-11
*
* */
public static Map<String,Object> findSeniorService(dispatchContext ctx,Map<String,? extends Object> context) {
//存放结果的map
Map<String,Object> result = FastMap.newInstance();
// GenericDelegator delegator = ctx.getDelegator();
String userid = (String) context.get("userid");
String salt = (String) context.get("salt");
Debug.logInfo("salt is ["+salt+"],userid is ["+userid+"] ","findSeniorService");
result.put("aaa","test_aaaaa");
result.put("bbb","test_bbbbb");
result.put("ccc","test_ccccc");
result.put("userid",userid);
return result;
}
}
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对应到指定的处理函数上。
增加如下:
<!-- 引擎接口 --> <request-map uri="httpService"> <event type="java" path="org.ofbiz.service.engine.HttpEngine" invoke="httpEngine"/> <response name="success" type="none"/> <response name="error" type="none"/> </request-map> <request-map uri="SOAPService"> <event type="soap"/> <response name="error" type="none"/> <response name="success" type="none"/> </request-map> <request-map uri="xmlrpc" track-serverhit="false" track-visit="false"> <event type="xmlrpc"/> <response name="error" type="none"/> <response name="success" type="none"/> </request-map>
现在只是发布了,但是我们必须要知道怎样请求才能得到这个服务,ofbiz提供了一个event来处理它,就是<handler name="soap" type="request" class="org.ofbiz.webapp.event.soAPEventHandler"/>,要使用它,你必须把这个定义在你的controller.xml文件中,当然,如果你已经引入了<include location="component://common/webcommon/WEB-INF/common-controller.xml"/>,那么就不需要了,这个里面已经定义好了。直接使用就行了。
通过在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文件是否可用
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。