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);}}