OCT 1. 增加JSONView功能

2. 完善用户词典值返回的数据内容
This commit is contained in:
黄昕 2024-02-09 10:06:56 +08:00
parent 75a12f4a73
commit 9227e3d939
11 changed files with 145 additions and 33 deletions

View File

@ -24,9 +24,13 @@ spring :
add-mappings: false
jackson :
date-format: yyyy-MM-dd HH:mm:ss.SSS
timezone: GMT+8
date-format : yyyy-MM-dd HH:mm:ss.SSS
timezone : GMT+8
default-property-inclusion: non_null
mapper :
default-view-inclusion: true
deserialization :
fail-on-unknown-properties: false
datasource:
#url : jdbc:mysql://xajhuang.com:3307/common_framework?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&useUnicode=true

View File

@ -8,6 +8,7 @@ import com.cmhi.cf.restapi.annotation.OperationLogAnnotation;
import com.cmhi.cf.restapi.pojo.base.BasePageResultResp;
import com.cmhi.cf.restapi.pojo.base.BasePagedReq;
import com.cmhi.cf.restapi.pojo.base.BaseRespStatus;
import com.cmhi.cf.restapi.pojo.dto.AddDictContentReq;
import com.cmhi.cf.restapi.pojo.dto.DictContentReq;
import com.cmhi.cf.restapi.pojo.dto.NewUserDictReq;
import com.cmhi.cf.restapi.pojo.dto.ProtocolReq;
@ -20,7 +21,9 @@ import com.cmhi.cf.restapi.pojo.vo.DictTypeResp;
import com.cmhi.cf.restapi.pojo.vo.NewUserDictResp;
import com.cmhi.cf.restapi.pojo.vo.ProtocolResp;
import com.cmhi.cf.service.DictionaryService;
import com.cmhi.cf.validation.group.JsonViewGroups;
import com.cmhi.cf.validation.group.ValidGroups;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
@ -161,16 +164,17 @@ public class SystemDictController {
}
}
@PostMapping("/user/dictContent")
@PostMapping("/user/getDictContent")
@ResponseBody
@EncryptionProtocol
@DecryptionProtocol
@OperationLogAnnotation(OperationModule = "字典模块", OperationType = "读取", OperationDesc = "获取所有用户字典")
@JsonView(JsonViewGroups.DictContentResponse.class)
@OperationLogAnnotation(OperationModule = "字典模块", OperationType = "读取", OperationDesc = "获取用户字典内容项")
public ProtocolResp<? extends BaseRespStatus> getUserDictionaryContent(@RequestBody ProtocolReq<DictContentReq> mr) {
List<String> validate = HelperUtils.validate(mr, ValidGroups.ProtocolCommonValid.class, ValidGroups.DictReqValid.class);
// 如果校验通过validate为空否则validate包含未校验通过项
if (validate.isEmpty()) {
DictContentReq req = mr.getMsgContent();
DictContentReq req = mr.getMsgContent();
List<DictContent> ret = dictionaryService.getUserDictionaryContent(req.getDictTpe());
return ProtocolResp.result(DictContentResp.builder()
@ -182,23 +186,24 @@ public class SystemDictController {
}
}
// @PutMapping("/user/addDictContent")
// @ResponseBody
// @EncryptionProtocol
// @DecryptionProtocol
// @OperationLogAnnotation(OperationModule = "字典模块", OperationType = "创建", OperationDesc = "新增用户字典内容项")
// public ProtocolResp<? extends BaseRespStatus> createNewDictionaryContent(@RequestBody ProtocolReq<NewUserDictReq> mr) {
// List<String> validate = HelperUtils.validate(mr, ValidGroups.ProtocolCommonValid.class, ValidGroups.DictReqValid.class);
// // 如果校验通过validate为空否则validate包含未校验通过项
// if (validate.isEmpty()) {
// NewUserDictReq req = mr.getMsgContent();
// dictionaryService.addNewUserDictionary(req.getDictName(), req.getDictAlias(), req.getDictRemark());
// return ProtocolResp.result(NewUserDictResp.builder()
// .dictName(req.getDictName())
// .build());
// } else {
// return ProtocolResp.result(ErrorCode.ERR_PARAMEXCEPTION, ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(),
// validate.toArray(new String[0]));
// }
// }
@PutMapping("/user/addDictContent")
@ResponseBody
@EncryptionProtocol
@DecryptionProtocol
@OperationLogAnnotation(OperationModule = "字典模块", OperationType = "创建", OperationDesc = "新增用户字典内容项")
public ProtocolResp<? extends BaseRespStatus> createNewDictionaryContent(@RequestBody ProtocolReq<AddDictContentReq> mr) {
List<String> validate = HelperUtils.validate(mr, ValidGroups.ProtocolCommonValid.class, ValidGroups.DictReqValid.class,
ValidGroups.DictReqContentValid.class);
// 如果校验通过validate为空否则validate包含未校验通过项
if (validate.isEmpty()) {
AddDictContentReq req = mr.getMsgContent();
//dictionaryService.addNewUserDictionary(req.getDictName(), req.getDictAlias(), req.getDictRemark());
return ProtocolResp.result(NewUserDictResp.builder()
.dictName(req.getDictName())
.build());
} else {
return ProtocolResp.result(ErrorCode.ERR_PARAMEXCEPTION, ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(),
validate.toArray(new String[0]));
}
}
}

View File

@ -92,7 +92,7 @@ public class DictData {
*/
@Schema(description = "创建时间")
@Column(value = "create_time")
private java.util.Date createTime;
private Timestamp createTime;
/**
* 更新者
@ -106,7 +106,7 @@ public class DictData {
*/
@Schema(description = "更新时间")
@Column(value = "update_time")
private java.util.Date updateTime;
private Timestamp updateTime;
/**
* 备注

View File

@ -4,7 +4,6 @@ import com.cmhi.cf.restapi.pojo.base.BaseRespStatus;
import com.cmhi.cf.restapi.pojo.vo.ProtocolResp;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
@ -39,8 +38,7 @@ import java.util.Stack;
public class HelperUtils {
public static final String UN_EXPECT_REGEX_CHARS = "^((?!(--|\\s|\\*|%|\\+|'|;])).)*$";
private static final ObjectMapper OBJ_MAPPER = new ObjectMapper().disable(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
private static final ObjectMapper OBJ_MAPPER = ObjectMapperProvider.getMapper();
public static String truncateString(String orgString, int maxLength) {
if (!stringNotEmptyOrNull(orgString) || orgString.length() <= maxLength) {
@ -182,7 +180,7 @@ public class HelperUtils {
}
public static ProtocolResp<? extends BaseRespStatus> jsonGetProtocolResp(String jsonString,
Class<?>[] subClass) throws JsonProcessingException {
Class<?>[] subClass) throws JsonProcessingException {
return OBJ_MAPPER.readValue(jsonString, new TypeReference<>() {
@Override
public Type getType() {
@ -217,6 +215,10 @@ public class HelperUtils {
return str != null && !str.isEmpty();
}
public static String meagreDbStringValue(String str) {
return stringNotEmptyOrNull(str) ? str : null;
}
public static String getHttpRequestHeaders(HttpServletRequest request) throws JsonProcessingException {
if (request != null) {
Map<String, String> headerMap = new HashMap<>(15);

View File

@ -0,0 +1,20 @@
package com.cmhi.cf.misc;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ObjectMapperProvider implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
ObjectMapperProvider.context = applicationContext;
}
public static ObjectMapper getMapper() {
return context.getBean(ObjectMapper.class);
}
}

View File

@ -0,0 +1,31 @@
package com.cmhi.cf.restapi.pojo.dto;
import com.cmhi.cf.restapi.pojo.po.DictDetails;
import com.cmhi.cf.validation.group.ValidGroups;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "AddDictContentReq", description = "新增用户字典内容请求参数")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AddDictContentReq {
@NotBlank(message = "dictTpe 字典名称不能为空", groups = ValidGroups.DictReqValid.class)
private String dictName;
@NotNull(message = "dictContent 字典内容值不能为 NULL", groups = ValidGroups.DictReqValid.class)
@Size(min = 1, max = 100, message = "字典内容至少需要指定 1 个,最多只能指定 100 个", groups = ValidGroups.DictReqValid.class)
private List<@NotNull(message = "dictContent 字典内容列表包含的对象不能为 NULL",
groups = ValidGroups.DictReqContentValid.class) DictDetails> dictContent;
}

View File

@ -1,20 +1,44 @@
package com.cmhi.cf.restapi.pojo.po;
import com.cmhi.cf.validation.group.JsonViewGroups;
import com.cmhi.cf.validation.group.ValidGroups;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonView;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.sql.Timestamp;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonPropertyOrder({"id", "title", "value", "sorted", "disabled"})
@JsonPropertyOrder({"id", "title", "value", "sorted", "cssClass", "listClass", "isDefault", "remark", "disabled"})
public class DictDetails {
@JsonView(JsonViewGroups.DictContentResponse.class)
private Long id;
@JsonView(JsonViewGroups.DictContentResponse.class)
private Boolean disabled;
@JsonView(JsonViewGroups.DictContentResponse.class)
private String createBy;
@JsonView(JsonViewGroups.DictContentResponse.class)
private Timestamp createTime;
@JsonView(JsonViewGroups.DictContentResponse.class)
private String upgradeBy;
@JsonView(JsonViewGroups.DictContentResponse.class)
private Timestamp upgradeTime;
@NotBlank(message = "title 字典内容不能为空", groups = ValidGroups.DictReqContentValid.class)
private String title;
@NotNull(message = "value 字典内容值不能为 NULL", groups = ValidGroups.DictReqContentValid.class)
private Integer value;
private Integer sorted;
private Boolean disabled;
private String cssClass;
private String listClass;
private Boolean isDefault;
private String remark;
}

View File

@ -6,6 +6,7 @@ import com.cmhi.cf.database.common.entity.DictType;
import com.cmhi.cf.database.common.mapper.DictDataMapper;
import com.cmhi.cf.database.common.mapper.DictTypeMapper;
import com.cmhi.cf.exception.CommonErrorCodeException;
import com.cmhi.cf.misc.HelperUtils;
import com.cmhi.cf.restapi.misc.ApiContextUtils;
import com.cmhi.cf.restapi.pojo.po.DictContent;
import com.cmhi.cf.restapi.pojo.po.DictDetails;
@ -181,6 +182,13 @@ public class DictionaryServiceImpl implements DictionaryService {
.title(m.getDictLabel())
.value(m.getDictValue())
.sorted(m.getDictSort())
.cssClass(HelperUtils.meagreDbStringValue(m.getCssClass()))
.listClass(HelperUtils.meagreDbStringValue(m.getListClass()))
.isDefault("Y".equals(m.getIsDefault()))
.createBy(m.getCreateBy())
.createTime(m.getCreateTime())
.upgradeBy(HelperUtils.meagreDbStringValue(m.getUpdateBy()))
.upgradeTime(m.getUpdateTime())
.disabled(false)
.build()));

View File

@ -0,0 +1,7 @@
package com.cmhi.cf.validation.group;
public class JsonViewGroups {
public static class CommonView {}
public static class DictContentResponse extends CommonView {}
public static class DictContentRequest extends CommonView {}
}

View File

@ -38,4 +38,7 @@ public interface ValidGroups {
interface DictReqValid extends BaseProtocolValid {
}
interface DictReqContentValid extends BaseProtocolValid {
}
}

View File

@ -44,7 +44,11 @@ INSERT IGNORE INTO rbac_resource_data VALUES (402, 'C083F441-9624-AF2C-B353-0768
INSERT IGNORE INTO rbac_resource_data VALUES (403, '953cea37-cc7a-4e7e-bd86-cb3c14daa8c4', '/api/dict/user/upgrade', '修改用户字典', 'POST', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (404, '247674fb-759d-4b01-8d35-2c0436d2ed99', '/api/dict/user/delete', '删除用户字典', 'DELETE', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (405, '54b43614-45c6-4672-a1d8-fa8f326d5e53', '/api/dict/user/allDict', '分页获取所有用户字典', 'POST', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (406, 'e441dbb4-b844-461c-a5f8-07ee3fa658cc', '/api/dict/user/dictContent', '获取用户字典详细信息', 'POST', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (406, 'e441dbb4-b844-461c-a5f8-07ee3fa658cc', '/api/dict/user/getDictContent', '获取用户字典详细信息', 'POST', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (407, 'c803571e-ba0d-4f3d-bcc8-76488b1af22a', '/api/dict/user/addDictContent', '新增用户字典详细信息', 'PUT', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (408, '99106f7a-6c4e-4438-ad40-65ed9417d0b4', '/api/dict/user/upgradeDictContent', '更新用户字典详细信息', 'POST', 0);
INSERT IGNORE INTO rbac_resource_data VALUES (409, 'a4c04e8f-d495-46ca-b67e-79cc9a91d623', '/api/dict/user/deleteDictContent', '删除用户字典详细信息', 'DELETE', 0);
-- ----------------------------
-- Records of role_resource
-- (role_id, resource_id, authorize)
@ -70,6 +74,10 @@ INSERT IGNORE INTO rbac_role_resource VALUES (1, 403, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 404, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 405, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 406, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 407, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 408, 1);
INSERT IGNORE INTO rbac_role_resource VALUES (1, 409, 1);
# INSERT IGNORE INTO role_resource (role_id, resource_id, authorize) VALUES (2, 1, 1);
# INSERT IGNORE INTO role_resource (role_id, resource_id, authorize) VALUES (2, 2, 1);
# INSERT IGNORE INTO role_resource (role_id, resource_id, authorize) VALUES (2, 3, 1);