协同办公OA业务系统数据集成(3)-基于OA(e-cology Jersey)定义Rest Api前端数据接口封装
系列文章目錄
第一章 OA&CRM數據集成業務場景及技術方案概述?
第二章 基于CRM(LiveBOS webservice接口)獲取數據
第三章 基于OA(ecology?Jersey)定義Rest Api前端數據接口封裝?
第四章 前端業務流程數據接口調用
前言
OA基于泛微E-cology9.0平臺構建,通過Jersey構建客戶數據api接口方式供前端業務調用,本章節介紹E-cology平臺接口定義過程。
一、Jersey是什么?
? ? ? ?Jersey是一個RESTFUL請求服務JAVA框架,與常規的JAVA編程使用的struts框架類似,它主要用于處理業務邏輯層。
? ? ? ?jersey常用注解:
@Path uri路徑 定義資源的訪問路徑,client通過這個路徑訪問資源。比如:@Path("user")@Produces 返回 指定返回MIME格式 資源按照那種數據格式返回,可取的值有:MediaType.APPLICATION_XXX。比如:@Produces(MediaType.APPLICATION_XML)@Consumes 接收入參 接受指定的MIME格式 只有符合這個參數設置的請求再能訪問到這個資源。比如@Consumes("application/x-www-form-urlencoded")@PathParam uri路徑參數 寫在方法的參數中,獲得請求路徑參數。比如:@PathParam("username") String userName?
?Jersey相關介紹https://www.jianshu.com/p/a9b0ebd4c3fe
二、E-cology 后端接口開發規范
1.架構圖
?
?
2.目錄結構
?
3.開發規范
1. 入參最好使用Map或者Entity對象,盡可能的與Http對象解耦
2. 出參最好使用Map或者Entity對象,不要直接返回單一數據或者String類型的JSON格式數據; 3. 不同功能的服務封裝在不同的Service中,用戶可以非常清晰地使用特定的Service
3. Service中定義的各個方法都有相應的命令對象(XXCmd)
4. Command中盡可能不要直接調用Service, 如果需要是公共類,請放到Biz目錄下;
5. Service必須有一個接口和一個實現類;
6. Service中不能包含具體的業務邏輯, 業務邏輯委托給具體的Command類;
7. Command中不允許直接調用Service方法;
三、客戶信息API接口定義
1.WSCustomerAction
package com.a.cs.web;import com.a.cs.service.WSCustomerService; import com.a.cs.service.impl.WSCustomerServiceImpl; import com.engine.common.util.ServiceUtil; import com.oneWorld.ecology.common.api.ApiMsgVo; import weaver.common.StringUtil; import weaver.conn.RecordSet; import weaver.integration.logging.Logger; import weaver.integration.logging.LoggerFactory; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import java.util.HashMap; import java.util.Map;public class WSCustomerAction {private Logger log = LoggerFactory.getLogger(this.getClass());@POST@Path("/getCustomerByWS")@Produces({MediaType.APPLICATION_JSON})public String getCustomerByWS(@Context HttpServletRequest request, @Context HttpServletResponse response) {String clientNo = request.getParameter("clientNo"); String depCode = request.getParameter("depCode"); String dataSource = request.getParameter("dataSource"); String flag = request.getParameter("flag"); String depId = request.getParameter("depId"); ApiMsgVo msgVo = new ApiMsgVo();if(clientNo == null){msgVo.setStatus(false);msgVo.setMsg("參數異常,未匹配數據");return msgVo.toJSONString();}Map<String, Object> arg = new HashMap<>(); //前端參數arg.put("clientNo",clientNo);arg.put("flag",flag);arg.put("dataSource",dataSource);arg.put("depCode",depCode);WSCustomerService infoService = ServiceUtil.getService(WSCustomerServiceImpl.class);//實例化service實現類Map<String, Object> map = infoService.getCustomerByWS(arg);log.debug("getCustomerByWS-result:"+map);if(map == null){msgVo.setStatus(false);msgVo.setMsg("接口獲取數據失敗!");return msgVo.toJSONString();}String status = String.valueOf(map.get("status")); //獲取數據成功與否狀態if(status != null && !"".equals(status)){if("-1".equals(status)){//獲取數據失敗msgVo.setStatus(false);msgVo.setMsg(String.valueOf(map.get("msg")));return msgVo.toJSONString();}}else{msgVo.setStatus(false);msgVo.setMsg("執行接口出現異常,返回參數status為空!");return msgVo.toJSONString();}msgVo.setData(map);return msgVo.toJSONString();}}2.WSCustomerService
package com.a.cs.service;import java.util.Map;public interface WSCustomerService {/*** 獲取人員信息* @return*/public Map<String,Object> getCustomerByWS (Map<String, Object> arg); }3.WSCustomerServiceImpl
package com.a.cs.service.impl;import com.a.cs.cmd.WSCustomerCmd; import com.a.cs.service.WSCustomerService; import com.engine.core.impl.Service;import java.util.Map;public class WSCustomerServiceImpl extends Service implements WSCustomerService {@Overridepublic Map<String, Object> getCustomerByWS(Map<String, Object> arg) {return (Map)this.commandExecutor.execute(new WSCustomerCmd(arg));} }4.WSCustomerCmd
package com.a.cs.cmd;import com.a.common.util.DataUtils; import com.engine.core.interceptor.Command; import com.engine.core.interceptor.CommandContext; import weaver.integration.logging.Logger; import weaver.integration.logging.LoggerFactory;import java.util.HashMap; import java.util.Map;public class WSCustomerCmd implements Command {private Logger log = LoggerFactory.getLogger(this.getClass());protected Map<String, Object> params;public WSCustomerCmd(Map<String, Object> params) {this.params = params;}@Overridepublic Object execute(CommandContext commandContext) {if(params == null)return null;String clientNo = String.valueOf(params.get("clientNo"));String depCode = String.valueOf(params.get("depCode"));String dataSource = String.valueOf(params.get("dataSource"));String flag = String.valueOf(params.get("flag"));Map<String, String> map = null;if(dataSource != null && !"".equals(dataSource)){if("0".equals(dataSource)){map = this.getClientInfoByWS(clientNo,depCode);}else{map = this.getClientInfoByLocal(clientNo,depCode,flag);}}return map;}public Map<String, String> getClientInfoByLocal(String clientNo,String depCode,String flag){Map<String, String> map = null;try {map = DataUtils.getClientInfoByLocal(clientNo,depCode,flag);} catch (Exception e) {map = new HashMap<>();map.put("status","-1");map.put("msg","獲取數據出現異常,請聯系管理員!");e.printStackTrace();}return map;}public Map<String, String> getClientInfoByWS(String clientNo,String depCode){Map<String,String> map = null;try {map = DataUtils.getCRMClientData(clientNo,depCode);} catch (Exception e) {map = new HashMap<>();map.put("status","-1");map.put("msg","獲取數據出現異常,請聯系管理員!");e.printStackTrace();}return map;}public WSCustomerCmd() {}public Map<String, Object> getParams() {return params;}public void setParams(Map<String, Object> params) {this.params = params;} }5.WSCustomerAction
package com.api.csco.workflow.cs;import javax.ws.rs.Path;@Path("/csco/workflow/cs") public class WSCustomerAction extends com.a.cs.web.WSCustomerAction{}?
總結
通過發布API接口,前端業務可以基于實際場景進行調用。
總結
以上是生活随笔為你收集整理的协同办公OA业务系统数据集成(3)-基于OA(e-cology Jersey)定义Rest Api前端数据接口封装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序员最反感的十件事,你有同感吗?
- 下一篇: 前端:Web技术的演化史