F2BPM 开发Api与RESTfull应用服务Api 层次关系及示例
Posted F2BPM流程引擎
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了F2BPM 开发Api与RESTfull应用服务Api 层次关系及示例相关的知识,希望对你有一定的参考价值。
目前越来越多的企业架构解决方案更加趋向于基于http协议“微服务”访问跨系统调用,而不使用统传的WebService调用,即通过RESTfull方式进行交互,更加轻量整合调用更加方便。本文档中所有F2BPM开发接口API都可以发布成RESTfull对外应用服务接口
RESTfull参数方式:
authorJson:主要是接口身份的认证相关参数,校验访问者的来源合法性
parmJson:请求业务数据的参数,比如分页参数,查询参数等
所有RESTfull都统一只有这两个Json参数
API开发接口与RestFull API服务发布代码示例
SmartFormRESTfullApi 在线表单的RestFull API服务
package com.f2bpm.controller.restfullapi; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import com.f2bpm.base.core.entity.AuthorEntity; import com.f2bpm.base.core.entity.MyInteger; import com.f2bpm.base.core.entity.PageParams; import com.f2bpm.base.core.enums.ExtensionValidateType; import com.f2bpm.base.core.utils.AppUtil; import com.f2bpm.base.core.utils.ExtensionValidate; import com.f2bpm.base.core.utils.FileDownUtil; import com.f2bpm.base.core.utils.FileUtil; import com.f2bpm.base.core.utils.JsonHelper; import com.f2bpm.base.core.utils.string.StringUtil; import com.f2bpm.base.core.utils.time.DateUtil; import com.f2bpm.system.admin.impl.model.Users; import com.f2bpm.system.security.utils.LogUtil; import com.f2bpm.system.security.web.WebHelper; import com.f2bpm.workflow.engine.api.entity.WorkflowInstanceContext; import com.f2bpm.workflow.engine.api.model.TaskInstance; import com.f2bpm.workflow.engine.api.model.TaskInstanceInfo; import com.f2bpm.workflow.engine.api.model.WorkflowForm; import com.f2bpm.workflow.engine.api.wapi.IWorkflowWAPIService; import com.f2bpm.workflow.engine.helper.WfWebHelper; import com.f2bpm.workflow.org.api.imodel.IUser; import com.f2bpm.workflow.org.api.iservice.IUserService; import com.f2bpm.workflow.smartForm.api.entity.BusObjectData; /* * 在线表单RestFull API服务接口 */ @Controller @RequestMapping("/restfullapi/smartForm/") public class SmartFormRESTfullApi { @Resource IUserService userService; public IWorkflowWAPIService WorkflowAPI = (IWorkflowWAPIService) AppUtil.getBean("WorkflowAPI"); /** * 获取在线表单数据 * * @param parmJson * @param authorJson * @param response */ @RequestMapping(value = "getOnlineFormData", method = RequestMethod.POST) public void getOnlineFormData(String parmJson, String authorJson, HttpServletResponse response) { String jsonResult = ""; try { JSONObject jsonJSONObject = JSONObject.fromObject(parmJson); AuthorEntity authorEntity = JsonHelper.jsonToObject(authorJson, AuthorEntity.class); String loginAccount = authorEntity.getLoginAccount(); // 流程实例ID String wiid = JsonHelper.getString(jsonJSONObject, "wiid"); // 表单业务键 String businessKey = JsonHelper.getString(jsonJSONObject, "businessKey"); List<BusObjectData> data = WorkflowAPI.getSmartFormApiManager().getBusObjectListData(wiid, businessKey); jsonResult = JsonHelper.objectToJSON(data); } catch (Exception ex) { jsonResult = JsonHelper.outResult(false, ex.toString()); } JsonHelper.write(response, jsonResult); } /** * 获取表单应用定义 * * @param parmJson * @param authorJson * @param response */ @RequestMapping(value = "getWorkflowForm", method = RequestMethod.POST) public void getWorkflowForm(String parmJson, String authorJson, HttpServletResponse response) { String jsonResult = ""; try { JSONObject jsonJSONObject = JSONObject.fromObject(parmJson); AuthorEntity authorEntity = JsonHelper.jsonToObject(authorJson, AuthorEntity.class); String loginAccount = authorEntity.getLoginAccount(); String wid = JsonHelper.getString(jsonJSONObject, "wid"); String appId = JsonHelper.getString(jsonJSONObject, "appId"); WorkflowForm form = null; if (StringUtil.isEmpty(wid)) { // 启动时 form = WorkflowAPI.getProcessDefManager().getWorkflowInfo(appId).getWorkflowFormRunned(); } else { // 运行时 form = WorkflowAPI.getProcessDefManager().getWorkflowInfo(wid, appId).getWorkflowFormRunned(); } jsonResult = JsonHelper.objectToJSON(form); } catch (Exception ex) { jsonResult = JsonHelper.outResult(false, ex.toString()); } JsonHelper.write(response, jsonResult); } /** * 获取表单手机端html模板 * * @param parmJson wid:启动时可为空 * @param authorJson * @param response */ @RequestMapping(value = "getWorkflowFormMobileHtml", method = RequestMethod.POST) public void getWorkflowFormMobileHtml(String parmJson, String authorJson, HttpServletResponse response) { String jsonResult = ""; try { JSONObject jsonJSONObject = JSONObject.fromObject(parmJson); AuthorEntity authorEntity = JsonHelper.jsonToObject(authorJson, AuthorEntity.class); String loginAccount = authorEntity.getLoginAccount(); String wid = JsonHelper.getString(jsonJSONObject, "wid");//启用时可为空 String appId = JsonHelper.getString(jsonJSONObject, "appId"); String html= null; if (StringUtil.isEmpty(wid)) { // 启动时 html = WorkflowAPI.getProcessDefManager().getWorkflowInfo(appId).getWorkflowFormRunned().getMobileTemplateContent(); } else { // 运行时 html = WorkflowAPI.getProcessDefManager().getWorkflowInfo(wid, appId).getWorkflowFormRunned().getMobileTemplateContent(); } jsonResult = html; } catch (Exception ex) { jsonResult = JsonHelper.outResult(false, ex.toString()); } JsonHelper.write(response, jsonResult); } }
WorkflowBusinessRESTfullApi 流程运行相关的服务接口
1 package com.f2bpm.controller.restfullapi; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 import java.util.List; 9 10 import javax.annotation.Resource; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 import net.sf.json.JSONObject; 15 16 import org.springframework.stereotype.Controller; 17 import org.springframework.web.bind.annotation.RequestMapping; 18 import org.springframework.web.bind.annotation.RequestMethod; 19 import org.springframework.web.bind.annotation.ResponseBody; 20 import org.springframework.web.multipart.MultipartFile; 21 import org.springframework.web.multipart.MultipartHttpServletRequest; 22 23 import com.f2bpm.base.core.entity.AuthorEntity; 24 import com.f2bpm.base.core.entity.MyInteger; 25 import com.f2bpm.base.core.entity.PageParams; 26 import com.f2bpm.base.core.enums.ExtensionValidateType; 27 import com.f2bpm.base.core.utils.AppUtil; 28 import com.f2bpm.base.core.utils.ExtensionValidate; 29 import com.f2bpm.base.core.utils.FileDownUtil; 30 import com.f2bpm.base.core.utils.FileUtil; 31 import com.f2bpm.base.core.utils.JsonHelper; 32 import com.f2bpm.base.core.utils.string.StringUtil; 33 import com.f2bpm.base.core.utils.time.DateUtil; 34 import com.f2bpm.system.admin.impl.model.Users; 35 import com.f2bpm.system.security.utils.LogUtil; 36 import com.f2bpm.system.security.web.WebHelper; 37 import com.f2bpm.workflow.engine.api.entity.WorkflowInstanceContext; 38 import com.f2bpm.workflow.engine.api.model.ActivityInfo; 39 import com.f2bpm.workflow.engine.api.model.TaskInstance; 40 import com.f2bpm.workflow.engine.api.model.TaskInstanceInfo; 41 import com.f2bpm.workflow.engine.api.wapi.IWorkflowWAPIService; 42 import com.f2bpm.workflow.engine.helper.WfWebHelper; 43 import com.f2bpm.workflow.org.api.imodel.IUser; 44 import com.f2bpm.workflow.org.api.iservice.IUserService; 45 /* 46 * 流程运行相关 RestFull API服务接口 47 */ 48 @Controller 49 @RequestMapping("/restfullapi/workflowBusiness/") 50 public class WorkflowBusinessRESTfullApi { 51 @Resource 52 IUserService userService; 53 54 public IWorkflowWAPIService WorkflowAPI = (IWorkflowWAPIService) AppUtil.getBean("WorkflowAPI"); 55 56 /** 57 * 查找任务实例 58 * @param id 59 * @param request 60 * @param response 61 * @return 62 * @throws IOException 63 */ 64 @ResponseBody 65 @RequestMapping(value = "getTask", method = RequestMethod.GET) 66 public TaskInstance getTask(String id, HttpServletRequest request, HttpServletResponse response) throws IOException { 67 TaskInstance task = new TaskInstance(); 68 id = "031d2404-94d1-4566-8ade-21126b620904"; 69 task = WorkflowAPI.getWorkTaskManager().getTaskInstanceByTaskId(id); 70 return task; 71 } 72 73 /** 74 * 获取待办列表 75 * @param authorJson 76 * @param parmJson 77 * @param request 78 * @param response 79 * @throws Exception 80 */ 81 @RequestMapping(value = "getTodoList", method = RequestMethod.GET) 82 public void getTodoList(String authorJson, String parmJson, HttpServletRequest request, HttpServletResponse response) throws Exception { 83 JSONObject jsonJSONObject = JSONObject.fromObject(parmJson); 84 PageParams pageParams = JsonHelper.jsonToObject(parmJson, PageParams.class); 85 AuthorEntity authorEntity = JsonHelper.jsonToObject(authorJson, AuthorEntity.class); 86 String loginAccount = authorEntity.getLoginAccount(); 87 MyInteger recordCount = new MyInteger(0); 88 MyInteger pageCount = new MyInteger(0); 89 String whereStr = JsonHelper.getString(jsonJSONObject, "whereStr"); 90 IUser user = userService.getUserByAccount(loginAccount); 91 List<TaskInstanceInfo> list = WorkflowAPI.getWorkTaskManager().getTodoList(user.getUserId(), whereStr.toString(), pageParams.getOrderBy(), pageParams.getPageIndex(), pageParams.getPageSize(), pageCount, recordCount, true); 92 String jsonString = JsonHelper.listToJSON(list); 93 String jsonResult = JsonHelper.convertToEasyUIJsonResult(jsonString, pageParams.getPageSize(), pageParams.getPageIndex(), recordCount.getValue(), pageCount.getValue()); 94 JsonHelper.write(response, jsonResult); 95 } 96 97 /** 98 * 已办列表 99 * @param authorJson 100 * @param parmJson 101 * @param request 102 * @param response 103 * @throws Exception 104 */ 105 @RequestMapping(value = "getDoneList", method = RequestMethod.POST) 106 public void getDoneList(String authorJson, String parmJson, HttpServletRequest request, HttpServletResponse response) throws Exception { 107 JSONObject jsonJSONObject = JSONObject.fromObject(parmJson); 108 PageParams pageParams = JsonHelper.jsonToObject(parmJson, PageParams.class); 109 AuthorEntity authorEntity = JsonHelper.jsonToObject(authorJson, AuthorEntity.class); 110 String loginAccount = authorEntity.getLoginAccount(); 111 MyInteger recordCount = new MyInteger(0); 112 MyInteger pageCount = new MyInteger(0); 113 String whereStr = JsonHelper.getString(jsonJSONObject, "whereStr"); 114 int isHistory = JsonHelper.getInt(jsonJSONObject, "isHistory"); 115 IUser user = userService.getUserByAccount(loginAccount); 116 List<TaskInstanceInfo> list = null; 117 if (isHistory == 1) { 118 // 归档中的列表 119 list = WorkflowAPI.getHistoryDataManager().getHistoryDoneList(user.getUserId(), whereStr.toString(), pageParams.getOrderBy(), pageParams.getPageIndex(), pageParams.getPageSize(), pageCount, recordCount, true); 120 } else { 121 // 进行中的已办 122 list = WorkflowAPI.getWorkTaskManager().getDoneList(user.getUserId(), whereStr.toString(), pageParams.getOrderBy(), pageParams.getPageIndex(), pageParams.getPageSize(), pageCount, recordCount, true); 123 } 124 125 String jsonString = JsonHelper.listToJSON(list); 126 String jsonResult = JsonHelper.convertToEasyUIJsonResult(jsonString, pageParams.getPageSize(), pageParams.getPageIndex(), recordCount.getValue(), pageCount.getValue()); 127 JsonHelper.write(response, jsonResult); 128 } 129 130 /** 131 * 响应发起流程 132 * @param authorJson 133 * @param parmJson 134 * @param request 135 * @param response 136 * @throws IOException 137 */ 138 @RequestMapping(value = "startWorkflow", method = RequestMethod.POST) 139 public void startWorkflow(String authorJson, String parmJson, HttpServletRequest request, HttpServletResponse response) throws IOException { 140 JSONObject jsonJSONObject = JSONObject.fromObject(parmJson); 141 AuthorEntity authorEntity = JsonHelper.jsonToObject(authorJson, AuthorEntity.class); 142 String loginAccount = authorEntity.getLoginAccount(); 143 // IUser user = userService.getUserByAccount(loginAccount); 144 String appId = JsonHelper.getString(jsonJSONObject, "appId"); 145 String wiid = JsonHelper.getString(jsonJSONObject, "wiid"); 146 String businessKey = JsonHelper.getString(jsonJSONObject, "businessKey"); 147 String title = JsonHelper.getString(jsonJSONObject, "title"); 148 String opinion = JsonHelper.getString(jsonJSONObject, "opinion"); 149 String jsonFormData = JsonHelper.getString(jsonJSONObject, "jsonFormData"); 150 StringBuilder message = new StringBuilder(); 151 boolean success = WorkflowAPI.getWorkflowEnactmentManager().startWorkflow(appId, wiid, businessKey, title, opinion, loginAccount, null, message, jsonFormData, null, 0, 0); 152 String jsonResult = JsonHelper.outResult(success, message.toString()); 153 JsonHelper.write(response, jsonResult); 154 } 155 156 /** 157 * 驳回流程 158 * @param parmJson 159 * @param authorJson 160 * @param response 161 */ 162 @RequestMapping(value = "rejectFlow", method = RequestMethod.POST) 163 public void rejectFlow(String parmJson, String authorJson, HttpServletResponse response) { 164 JSONObject jsonJSONObject = JSONObject.fromObject(parmJson); 165 AuthorEntity authorEntity = JsonHelper.jsonToObject(authorJson, AuthorEntity.class); 166 String loginAccount = authorEntity.getLoginAccount(); 167 String taskId = JsonHelper.getString(jsonJSONObject, "taskId"); 168 String opinion = JsonHelper.getString(jsonJSONObject, "opinion"); 169 StringBuilder message = new StringBuilder(); 170 boolean success = WorkflowAPI.getWorkflowEnactmentManager().rejectWorkflow(taskId, loginAccount, opinion, true, null, message); 171 String jsonResult = JsonHelper.outResult(success, message.toString()); 172 JsonHelper.write(response, jsonResult); 173 } 174 175 /** 176 * 作废流程实例 177 * 178 * @param parmJson 179 * @param authorJson 180 * @param response 181 */ 182 @RequestMapping(value = "invalidFlow", method = RequestMethod.POST) 183 public void invalidFlow(String parmJson, String authorJson, HttpServletResponse response) { 184 JSONObject jsonJSONObject = JSONObject.fromObject(parmJson); 185 AuthorEntity authorEntity = JsonHelper.jsonToObject(authorJson, AuthorEntity.class); 186 String loginAccount = authorEntity.getLoginAccount(); 187 String taskId = JsonHelper.getString(jsonJSONObject, "taskId"); 188 String opinion = JsonHelper.getString(jsonJSONObject, "opinion"); 189 TaskInstance taskInstance = WorkflowAPI.getWorkTaskManager().getTaskInstanceByTaskId(taskId); 190 String wiid = taskInstance.getWorkflowInstanceId(); 191 StringBuilder message = new StringBuilder(); 192 IUser user = WorkflowAPI.getOrgEngineManager().getUserService().getUserByAccount(loginAccount); 193 boolean success = WorkflowAPI.getWorkflowEnactmentManager().invalidWorkflowInstance(wiid, opinion, user); 194 String jsonResult = JsonHelper.outResult(success, message.toString()); 195 JsonHelper.write(response, jsonResult); 196 } 197 198 199 200 /** 201 * 审批执行代办任务 202 * @param authorJson 203 * @param parmJson 204 * @param response 205 */ 206 @RequestMapping(value = "approvalTask", method = RequestMethod.POST) 207 public void approvalTask(String authorJson, String parmJson, HttpServletResponse response) { 208 JSONObject jsonJSONObject = JSONObject.fromObject(parmJson); 209 AuthorEntity authorEntity = JsonHelper.jsonToObject(authorJson, AuthorEntity.class); 210 String loginAccount = authorEntity.getLoginAccount(); 211 String taskId = JsonHelper.getString(jsonJSONObject, "taskId"); 212 String opinion = JsonHelper.getString(jsonJSONObject, "opinion"); 213 StringBuilder message = new StringBuilder(); 214 boolean success = WorkflowAPI.getWorkflowEnactmentManager().sendWorkflowByTaskId(taskId, loginAccount, opinion, message); 215 String jsonResult = JsonHelper.outResult(success, message.toString()); 216 JsonHelper.write(response, jsonResult); 217 } 218 219 /** 220 * 获取任务指定节点的供选择人(计算出节点参与者) 221 * @param authorJson 222 * @param parmJson 223 * @param response 224 */ 225 @RequestMapping(value = "getActivityUsers", method = RequestMethod.POST) 226 public void getActivityUsers(String authorJson, String parmJson, HttpServletResponse response) { 227 JSONObject jsonJSONObject = JSONObject.fromObject(parmJson); 228 AuthorEntity authorEntity = JsonHelper.jsonToObject(authorJson, AuthorEntity.class); 229 String loginAccount = authorEntity.getLoginAccount(); 230 String taskId = JsonHelper.getString(jsonJSONObject, "taskId"); 231 String activityId = JsonHelper.getString(jsonJSONObject, "activityId"); 232 StringBuilder message = new StringBuilder(); 233 WorkflowInstanceContext wfContext=WorkflowAPI.getWorkflowEnactmentManager().getWorkflowInstanceContextOnTodo(taskId, loginAccount); 234 TaskInstance task = WorkflowAPI.getWorkTaskManager().getTaskInstanceByTaskId(taskId); 235 ActivityInfo activityInfo=WorkflowAPI.getProcessDefManager().getActivityInfo(task.getWorkflowId(), activityId); 236 List<IUser> list= WorkflowAPI.getWorkflowEnactmentManager().getListActorUserResultByActivityInfo(wfContext,activityInfo); 237 String jsonResult =JsonHelper.objectToJSON(list); 238 JsonHelper.write(response, jsonResult); 239 } 240 241 /** 242 * 上传在线表单附件 上传在线表单附件, 返回字段值单个附件值,表单附件的字段值格式: 243 * [{filePathName:"/attachments/workflow/files/20170714215409547.txt" 244 * ,fileName:"TestClient.txt"}] 245 * 246 * @param request 247 * @param response 248 */ 249 @RequestMapping("uploadFile") 250 public void uploadFile(HttpServletRequest request, HttpServletResponse response) { 251 String jsonResult = ""; 252 MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request; 253 MultipartFile fileUpload = mRequest.getFile("FileUpload"); 254 if (!fileUpload.isEmpty()) { 255 256 try { 257 String uploadType = WfWebHelper.query("UploadType", ""); 258 259 String path = FileDownUtil.getWorkflowFilePath(); 260 String uploadFileName = fileUpload.getOriginalFilename(); 261 String fileExtension = uploadFileName.substring(uploadFileName.lastIndexOf(".")); 262 263 if (!StringUtil.isNullOrWhiteSpace(uploadType)) { 264 path = path + uploadType + "/"; 265 if (!FileUtil.isExistDirectory(path)) { 266 FileUtil.createFolder(path); 267 } 268 } 269 270 if (!ExtensionValidate.validateExtension(fileExtension, ExtensionValidateType.FileExtension)) { 271 jsonResult = JsonHelper.outResult(false, fileExtension + ",文件类型不允许上传!"); 272 JsonHelper.write(response, jsonResult); 273 return; 274 } 275 uploadFileName = DateUtil.getCurrentDateTime("yyyyMMddHHmmssSSS") + fileExtension; 276 String filePathName = path + uploadFileName; 277 if (FileUtil.isExistFile(filePathName)) { 278 FileUtil.deleteFile(filePathName); 279 } 280 File filePath = new File(filePathName);// 上传地址 281 fileUpload.transferTo(filePath); 282 283 int index = file以上是关于F2BPM 开发Api与RESTfull应用服务Api 层次关系及示例的主要内容,如果未能解决你的问题,请参考以下文章
[Go] 从零开始项目-基于gin框架打造restfull风格API