1.在開發過程中我們難免遇見會存在需要將集合傳遞到后端的情況,那么這里就有一些如下的注意事項,如以下代碼:
// 新增@action.boundasync addQuestion(formData) {var theList = this.questionAnswerList;var questionAnswerListArray = new Array();for(var i=0;i<theList.length;i++){if(theList[i]){questionAnswerListArray.push(toJS(theList[i]));}}const updateFormData = {...formData,questionAnswerList:JSON.stringify(questionAnswerListArray),}const res = await WeaTools.callApi("/api/question/submit", "POST", updateFormData,"json");if (res.code === "1") {message.success(res.message);this.fetchDataDialog();} else {message.error(res.message);}}
我們要對頁面所存存儲的集合對象進行toJS的操作,因為我們這里用的是@action,他會自動對應頁面對象中的數據修改,但是這種對象例如集合是前端獨有的數據類型,后端是沒有這種數據進行接收的,所以如果不進行toJS操作,并且將集合轉為JSON字符串,那么后端接收到的很有可能就是’Object object'的這種字符串;通過以上操作才能將集合轉為我們需要的JSON字符串,然后在后端,我們可以通過?String demoJson = (String) stringObjectMap.get("demoList");
? ? ? ? ? ? ? ? List<Demo> demoList = JSON.parseObject(demoJson, new TypeReference<List<Demo>>() {});解析為我們需要的集合。
2.這是一個前后端接口的一些方法和操作,具體的可以看看:里面有POST、GET的具體實現泛微e9開發 編寫前端請求后端接口方法以及編寫后端接口_泛微后端接口文檔-CSDN博客https://blog.csdn.net/Liron_wg/article/details/144161262
這里需要注意的因為ecology的版本有可能不同,里面的get傳遞參數和后端獲取參數有一些差異;博主這里使用上面的get操作,按照他提供的傳參和獲取參數是無法獲取到的。?所以博主這里提供另外一種方法來獲取參數:
const url = new URL('/api/test/testExport', window.location.origin);url.searchParams.append('userId', this.userId);url.searchParams.append('departId', this.departId);url.searchParams.append('examId', this.examId);// 使用 GET 請求fetch(url, {method: 'GET',headers: {'Content-Type': 'application/json', // 不需要設置 Content-Type,因為是 GET 請求},}).then(response => {console.log(response);})
@Path("/testExport")@GET@Produces(MediaType.APPLICATION_OCTET_STREAM)public String testExport(@Context HttpServletRequest request, @Context HttpServletResponse response) {try {String userId = Util.null2String(request.getParameter("userId"));String departId = Util.null2String(request.getParameter("departId"));String examId = Util.null2String(request.getParameter("examId"));// 獲取當前用戶的信息User user = HrmUserVarify.getUser(request, response);Map<String,Object> resultMap =new HashMap<>();resultMap.put("code","1");resultMap.put("message","成功!");return JSONObject.toJSONString(resultMap);} catch (Exception e) {Map<String,Object> resultMap =new HashMap<>();resultMap.put("code","0");resultMap.put("message","失敗!");return JSONObject.toJSONString(resultMap);}}