public ErrorMessage(String message) { jsonObject.put("code", 200); jsonObject.put("message", message); jsonObject.put("success", false); jsonObject.put("type", "error"); jsonObject.put("data", null); } public JSONObject getMessage() { return jsonObject; }什么意思
时间: 2024-04-06 09:32:06 浏览: 23
这段代码定义了 `ErrorMessage` 类的构造函数和一个返回 `JSONObject` 类型的 `getMessage()` 方法。
在构造函数中,传入一个字符串类型的参数 `message`,然后将 code、message、success、type 和 data 这些键值对分别存储到 `jsonObject` 对象中。其中,code 的值为 200,表示请求成功;message 的值为传入的参数 `message`;success 的值为 false,表示请求失败;type 的值为 "error",表示错误类型;data 的值为 null,表示没有数据返回。
在 `getMessage()` 方法中,返回 `jsonObject` 对象。该方法可以在其他类中被调用,以获取 `jsonObject` 对象并将其转换为 JSON 字符串,用于返回给客户端。
相关问题
package com.example.yin.common; import com.alibaba.fastjson.JSONObject; public class ErrorMessage { JSONObject jsonObject = new JSONObject();什么意思
这段代码定义了一个名为 `ErrorMessage` 的类,其中包含一个名为 `jsonObject` 的成员变量,其类型为 `JSONObject`。在类的构造函数中,将 `code` 和 `mes` 两个键值对分别存储到 `jsonObject` 对象中。这段代码使用了 FastJSON 库,用于将 JSON 字符串转换为 Java 对象。
Map<String, OrganizationEo> organizationEoMap = comDataComponent.getAllOrganizationMap(); Map<String, UserEo> userEoMap =comDataComponent.getUserMapByUid(); String code = ""; SchemaColumnRuleResponseDto responseDto = new SchemaColumnRuleResponseDto(); BeanUtils.copyProperties(requestDto,responseDto); //用户是否存在 if(!CollectionUtils.isEmpty(requestDto.getColumnPermissionUserList())){ for (String userUid : requestDto.getColumnPermissionUserList()) { if(!userEoMap.containsKey(userUid)){ code = Message.NOT_EXIST.getCode(); responseDto.setResultCode(code); responseDto.setResultMsg(userUid+messageInfo.getMessageByCode(code)); return responseDto; } } schemaColumnRuleEo.setColumnPermissionUserList(JSONObject.toJSONString(requestDto.getColumnPermissionUserList())); } //用户组织是否存在 if(!CollectionUtils.isEmpty(requestDto.getColumnPermissionOrgList())){ List<String> orgList = requestDto.getColumnPermissionOrgList(); for (String orgCode : orgList) { if(!organizationEoMap.containsKey(orgCode)){ code = Message.NOT_EXIST.getCode(); responseDto.setResultCode(code); responseDto.setResultMsg(orgCode+messageInfo.getMessageByCode(code)); return responseDto; } } schemaColumnRuleEo.setColumnPermissionOrgList(JSONObject.toJSONString(orgList)); } return new SchemaColumnRuleResponseDto();代码优化最优的
优化代码最优的方式是将重复的代码提取出来,使用方法进行复用,可以使用一个方法来检查用户和用户组织是否存在。同时,可以使用一个变量来存储错误代码和错误信息,避免重复赋值。最终优化后的代码如下:
```
public SchemaColumnRuleResponseDto checkColumnPermission(SchemaColumnRuleRequestDto requestDto, Map<String, OrganizationEo> organizationEoMap, Map<String, UserEo> userEoMap) {
SchemaColumnRuleResponseDto responseDto = new SchemaColumnRuleResponseDto();
BeanUtils.copyProperties(requestDto, responseDto);
String errorCode = "";
if (!CollectionUtils.isEmpty(requestDto.getColumnPermissionUserList())) {
errorCode = checkExistence(userEoMap, requestDto.getColumnPermissionUserList(), "用户");
if (!StringUtils.isEmpty(errorCode)) {
responseDto.setResultCode(errorCode);
responseDto.setResultMsg(messageInfo.getMessageByCode(errorCode));
return responseDto;
}
schemaColumnRuleEo.setColumnPermissionUserList(JSONObject.toJSONString(requestDto.getColumnPermissionUserList()));
}
if (!CollectionUtils.isEmpty(requestDto.getColumnPermissionOrgList())) {
errorCode = checkExistence(organizationEoMap, requestDto.getColumnPermissionOrgList(), "组织");
if (!StringUtils.isEmpty(errorCode)) {
responseDto.setResultCode(errorCode);
responseDto.setResultMsg(messageInfo.getMessageByCode(errorCode));
return responseDto;
}
schemaColumnRuleEo.setColumnPermissionOrgList(JSONObject.toJSONString(requestDto.getColumnPermissionOrgList()));
}
return responseDto;
}
private String checkExistence(Map<String, ?> map, List<String> list, String type) {
for (String key : list) {
if (!map.containsKey(key)) {
return Message.NOT_EXIST.getCode();
}
}
return "";
}
```