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

  1. <request-map uri="httpService">
  2. <event type="java" path="org.ofbiz.service.engine.HttpEngine" invoke="httpEngine"/>
  3. <response name="success" type="none"/>
  4. <response name="error" type="none"/>
  5. </request-map>
  6. <request-map uri="ExamWebService">
  7. <event type="soap"/>
  8. <response name="error" type="none"/>
  9. <response name="success" type="none"/>
  10. </request-map>
  11. <request-map uri="xmlrpc" track-serverhit="false" track-visit="false">
  12. <event type="xmlrpc"/>
  13. <response name="error" type="none"/>
  14. <response name="success" type="none"/>
  15. </request-map>

通过在controller.xml文件上的支持,才能将httpService,SOAPService,xmlrpc这些服务对外提供

2、定义ofbiz的service(servicedef下面的server.xml):这个是ofbiz赖以骄傲的设计方式。她可以将所有内部实体对象的CRUD都使用service的方式提供,不同系统之间可以通过互相调用service来完成业务操作。如果想将ofbiz的某个服务开放成webservice只需要将我们定义service文件中的service属性中的export设定为true即可。

  1. <service name="findAllOrderPlanList" engine="java"
  2. location="org.eheluo.ecloud.service.webService.WebService" invoke="findAllOrderPlanList"
  3. export="true" auth="false">
  4. <attribute name="examKsZqs" mode="IN" type="String" optional="false" />
  5. <attribute name="result" mode="OUT" type="String" />
  6. </service>

上面代码表示:将:org.eheluo.ecloud.service.webService.WebService类中的findAllOrderPlanList作为soap接口提供出去。attribute的mode属性:IN表示输入参数,OUT表示输出参数,INOUT表示输入输出同用。如果auth="true",则必须做用户验证,否则会报错:org.ofbiz.service.ServiceAuthException: User authorization is required for this service: findAllOrderPlanList

3、实际业务类:(注意:入参和出参都是map类型)

  1. public class WebService extends BaseService {
  2.  
  3. public static Map<String, Object> findAllOrderPlanList(DispatchContext dc, Map<String, ? extends Object> context)
  4. throws ServiceAuthException {
  5. Delegator delegator = dc.getDelegator();
  6. List<EntityCondition> ec = FastList.newInstance();
  7. String examKsZqs= (String) context.get("examKsZqs");
  8. if(examKsZqs != null && !"".equals(examKsZqs)){
  9. ec.add(EntityCondition.makeCondition("examKsZqs", EntityOperator.EQUALS, Integer.valueOf(examKsZqs)));
  10. }
  11. Map<String, Object> map = findAll(delegator, "OrderPlan",
  12. ec.size() > 0 ? EntityCondition.makeCondition(ec,
  13. EntityOperator.AND) : null, UtilMisc.toList("examKsZqs DESC"),dc,context);
  14. List<GenericValue> orderList = (List<GenericValue>) map.get("modellist");
  15. JSONArray jsonArray = new JSONArray();
  16. if (orderList != null && orderList.size() > 0) {
  17. for (GenericValue gv : orderList) {
  18. JSONObject jsonObject = new JSONObject();
  19. jsonObject.put("guid", gv.getString("guid"));
  20. jsonObject.put("examKsZqs", gv.getInteger("examKsZqs"));
  21. jsonObject.put("bmbName", gv.getString("bmbName"));
  22. jsonArray.add(jsonObject);
  23. }
  24. }
  25. Map<String, Object> resultMap = new HashMap<String, Object>();
  26. resultMap.put("result", jsonArray.toString());
  27. return resultMap;
  28. }
  29. }

通过这三步就可以对外发布服务了,wsdl的访问方式:http://localhost:8080/ecloud/control/ExamWebService?wsdl

访问结果:

点击方法名得到结果:

类似这样的结果,则表示webservice发布成功。

4、测试:

(1):SOAPWebService

1):java测试

import org.apache.axiom.om.OMAbstractFactory;
  import org.apache.axiom.om.OMAttribute;
  import org.apache.axiom.om.OMElement;
  import org.apache.axiom.om.OMFactory;
  import org.apache.axiom.om.OMNamespace;
  import org.apache.axis2.addressing.EndpointReference;
  import org.apache.axis2.client.Options;
  import org.apache.axis2.client.ServiceClient;

String endpoint = "http://localhost:8080/ecloud/control/ExamWebService";
  String tempuri = "http://ofbiz.apache.org/service/";

  1. OMFactory fac = OMAbstractFactory.getOMFactory();
  2. OMNamespace omNs = fac.createOMNamespace(tempuri, "");
  3. ServiceClient sc = new ServiceClient();
  4. Options opts = new Options();
  5. opts.setTo(new EndpointReference(endpoint));
  6. opts.setAction("findAllOrderPlanList");
  7. sc.setOptions(opts);
  8. OMElement method = fac.createOMElement("findAllOrderPlanList", omNs);
  9. OMElement parameters = fac.createOMElement("map-Map", omNs);
  10. parameters.addChild(createMapEntry(fac, omNs, "login.username", "admin"));
  11. parameters.addChild(createMapEntry(fac, omNs, "login.password", "ofbiz"));
  12. parameters.addChild(createMapEntry(fac, omNs, "examKsZqs", examKsZqs));
  13. method.addChild(parameters);
  14. OMElement res = sc.sendReceive(method);
  15. System.out.println(res);
  1. private static OMElement createMapEntry(OMFactory fac, OMNamespace omNs, String key, String val) {
  2. OMElement mapEntry = fac.createOMElement("map-Entry", omNs);
  3.  
  4. // create the key
  5. OMElement mapKey = fac.createOMElement("map-Key", omNs);
  6. OMElement keyElement = fac.createOMElement("std-String", omNs);
  7. OMAttribute keyAttribute = fac.createOMAttribute("value", null, key);
  8. mapKey.addChild(keyElement);
  9. keyElement.addAttribute(keyAttribute);
  10.  
  11. // create the value
  12. OMElement mapValue = fac.createOMElement("map-Value", omNs);
  13. OMElement valElement = fac.createOMElement("std-String", omNs);
  14. OMAttribute valAttribute = fac.createOMAttribute("value", null, val);
  15. mapValue.addChild(valElement);
  16. valElement.addAttribute(valAttribute);
  17.  
  18. // attach to map-Entry
  19. mapEntry.addChild(mapKey);
  20. mapEntry.addChild(mapValue);
  21.  
  22. return mapEntry;
  23. }

得到结果:

2):Postman测试

  1. <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  2. <SOAP-ENV:Body>
  3. <findAllOrderPlanList xmlns="http://ofbiz.apache.org/service/">
  4. <map-Map>
  5. <map-Entry>
  6. <map-Key>
  7. <std-String value="login.username"></std-String>
  8. </map-Key>
  9. <map-Value>
  10. <std-String value="admin"></std-String>
  11. </map-Value>
  12. </map-Entry>
  13. <map-Entry>
  14. <map-Key>
  15. <std-String value="login.password"></std-String>
  16. </map-Key>
  17. <map-Value>
  18. <std-String value="ofbiz"></std-String>
  19. </map-Value>
  20. </map-Entry>
  21. <map-Entry>
  22. <map-Key>
  23. <std-String value="examKsZqs"></std-String>
  24. </map-Key>
  25. <map-Value>
  26. <std-String value=""></std-String>
  27. </map-Value>
  28. </map-Entry>
  29. </map-Map>
  30. </findAllOrderPlanList>
  31. </SOAP-ENV:Body>
  32. </SOAP-ENV:Envelope>

得到结果:

(2):xmlrpc

java测试:

import org.apache.xmlrpc.client.XmlRpcClient;
  import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

  1. String endpoint = "http://localhost:8080/ecloud/control/xmlrpc";
  2.  
  3. XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
  4. config.setServerURL(new URL(endpoint));
  5. config.setEnabledForExceptions(true);
  6. config.setEnabledForExtensions(true);
  7.  
  8. XmlRpcClient client = new XmlRpcClient();
  9. client.setConfig(config);
  10.  
  11. Map paramMap = new HashMap();
  12. paramMap.put("examKsZqs", examKsZqs);
  13. paramMap.put("login.username", "admin");
  14. paramMap.put("login.password", "ofbiz");
  15.  
  16. Object[] params = new Object[]{paramMap};
  17. Map result = (Map) client.execute("findAllOrderPlanList", params);
  18. System.out.println(result.toString());

得到结果:

ofbiz webservice 例解的更多相关文章

  1. 基础拾遗------webservice详解

    基础拾遗 基础拾遗------特性详解 基础拾遗------webservice详解 基础拾遗------redis详解 基础拾遗------反射详解 基础拾遗------委托详解 基础拾遗----- ...

  2. SQL 连接 JOIN 例解。(左连接,右连接,全连接,内连接,交叉连接,自连接)

    SQL 连接 JOIN 例解.(左连接,右连接,全连接,内连接,交叉连接,自连接) 最近公司在招人,同事问了几个自认为数据库可以的应聘者关于库连接的问题,回答不尽理想-现在在这写写关于它们的作用假设有 ...

  3. 图文例解C++类的多重继承与虚拟继承

    文章导读:C++允许为一个派生类指定多个基类,这样的继承结构被称做多重继承. 在过去的学习中,我们始终接触的单个类的继承,但是在现实生活中,一些新事物往往会拥有两个或者两个以上事物的属性,为了解决这个 ...

  4. 《挑战30天C++入门极限》图文例解C++类的多重继承与虚拟继承

        图文例解C++类的多重继承与虚拟继承 在过去的学习中,我们始终接触的单个类的继承,但是在现实生活中,一些新事物往往会拥有两个或者两个以上事物的属性,为了解决这个问题,C++引入了多重继承的概念 ...

  5. Axis2开发webservice详解

    Axis2开发webservice详解 标签: javawebserviceAxis2 2015-08-10 10:58 1827人阅读 评论(0) 收藏 举报  分类: JAVA(275)  服务器 ...

  6. Webservice详解

    WebService是什么? 1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 2. 一个跨语言.跨平台的规范(抽象) 3. 多个跨平台.跨语言的应用间通信整合的方案(实际) ...

  7. Android平台调用WebService详解

    上篇文章已经对Web Service及其相关知识进行了介绍(Android开发之WebService介绍 ),相信有的朋友已经忍耐不住想试试在Android应用中调用Web Service.本文将通过 ...

  8. 例解 autoconf 和 automake 生成 Makefile 文件

    本文介绍了在 linux 系统中,通过 Gnu autoconf 和 automake 生成 Makefile 的方法.主要探讨了生成 Makefile 的来龙去脉及其机理,接着详细介绍了配置 Con ...

  9. WebService详解(二)

    WsExplorer和Tcp/Ip Monitor工具本身就存在于eclipse和MyEclipse中  使用工具的原因:  1.  使用工具可以更好的了解WebService请求的过程  2.  使 ...

随机推荐

  1. 【Vim编辑器】基本命令

    前言 工作中免不了会使用到vim编辑文档,总会觉得不好上手,遂从网上找到一篇说明文档整理如下,共勉. 原文地址: https://www.cnblogs.com/shiyanlou/archive/2 ...

  2. Python基础教程(020)--集成开发环境IDE简介--Pycharm

    前言 学会掌握Pycharm工具 内容 集成了开发软件需要的所有工具 1,图形用户界面 2,代码编译器(支持代码补全,自动缩进) 3,编译器,解释器 4,调试器(断点,单步执行) Pycharm介绍 ...

  3. Axure RP 8.0软件安装教程

    Axure8.0(32/64)位下载地址: 链接:https://pan.baidu.com/s/1qYSLkKW 密码:skaw 软件介绍: Axure RP是一个专业的快速原型设计工具,让负责定义 ...

  4. 2018-2019-2 网络对抗技术 20165206 Exp 8 Web基础

    - 2018-2019-2 网络对抗技术 20165206 Exp 8 Web基础 - 实验任务 (1).Web前端HTML(0.5分) 能正常安装.启停Apache.理解HTML,理解表单,理解GE ...

  5. Redis入门很简单之七【使用Jedis实现客户端Sharding】

    Redis入门很简单之七[使用Jedis实现客户端Sharding] 博客分类: NoSQL/Redis/MongoDB redisjedisspringsharding分片 <一>. 背 ...

  6. Classic IPC Problems 经典的进程间通信问题

    The Producer-Consumer Problem Presenter Notes: 生产者消费者问题(英语:Producer-consumer problem),也称有限缓冲问题(英语:Bo ...

  7. drf基础

    1.什么是编程? 数据结构和算法的结合 2.什么是REST? 同一个功能会产生五花八门的url(把查看单条记录和查看多条记录都看成是一个功能),而且响应回去的数据也没有同一的格式规范,这就造成了前后端 ...

  8. Java迭代器模式

    迭代器模式是Java和.Net编程环境中非常常用的设计模式.此模式用于以顺序方式访问集合对象的元素,而不需要知道其底层表示. 迭代器模式属于行为模式类别. 实现实例 在这个实例中,将创建一个Itera ...

  9. 杯子(glass)

    题目描述 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把一个瓶子的水全部倒 ...

  10. python基础之运算符和编码

    while循环 什么是循环? 就是不断的重复做一件事 while --关键字 后边跟条件 :还有循环体. 条件体为真,循环体内执行,为假不执行 while else 两者为一体的,相当于 if els ...