Commit 007035bd by s_guodong

1.增加时间校验

2.优化返回前端时null值处理
parent 974e9efd
...@@ -9,6 +9,8 @@ import org.springframework.context.annotation.Bean; ...@@ -9,6 +9,8 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import java.util.Date;
@Configuration @Configuration
public class JsonConfig { public class JsonConfig {
...@@ -26,6 +28,7 @@ public class JsonConfig { ...@@ -26,6 +28,7 @@ public class JsonConfig {
SimpleModule simpleModule = new SimpleModule(); SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance); simpleModule.addSerializer(Long.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance); simpleModule.addSerializer(Long.TYPE, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
simpleModule.addDeserializer(Date.class, new MyDateDeSerialized());
objectMapper.registerModule(simpleModule); objectMapper.registerModule(simpleModule);
// 反序列化的时候如果多了其他属性,不抛出异常 // 反序列化的时候如果多了其他属性,不抛出异常
......
package com.ceb.gjjs.mda.config;
import com.brilliance.eibs.util.StringUtil;
import com.ceb.gjjs.mda.util.ValidatorUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import javax.validation.constraints.Pattern;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyDateDeSerialized extends JsonDeserializer {
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
Date date = null;
String source = p.getText().trim();
String currentName = p.getCurrentName();
Object currentValue = p.getCurrentValue();
Class<?> aClass = currentValue.getClass();
Field declaredField = null;
try {
declaredField = aClass.getDeclaredField(currentName);
} catch (NoSuchFieldException e) {
}
declaredField.setAccessible(true);
if (StringUtil.isNotEmpty(source)) {
String format = declaredField.getAnnotation(JsonFormat.class).pattern();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
try {
date = simpleDateFormat.parse(source);
} catch (ParseException e) {
Pattern patternAnnotation = declaredField.getAnnotation(Pattern.class);
ValidatorUtil.CHECK_ERR_MSG.set(patternAnnotation == null ? "时间格式不正确" : patternAnnotation.message());
return null;
}
}
return date;
}
}
...@@ -11,9 +11,9 @@ import java.io.Serializable; ...@@ -11,9 +11,9 @@ import java.io.Serializable;
* @Author s_guodong * @Author s_guodong
* @Date 2023/8/10 * @Date 2023/8/10
*/ */
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
@Data @Data
public class RespondBaseVo implements Serializable { public class RespondBaseVo implements Serializable {
@JsonInclude(JsonInclude.Include.NON_NULL)
private String total; private String total;
} }
...@@ -219,6 +219,8 @@ public class ReflectUtil { ...@@ -219,6 +219,8 @@ public class ReflectUtil {
f.set(respondVo, 0D); f.set(respondVo, 0D);
} else if (tType.isAssignableFrom(Integer.TYPE) || tType.isAssignableFrom(Integer.class)) { } else if (tType.isAssignableFrom(Integer.TYPE) || tType.isAssignableFrom(Integer.class)) {
f.set(respondVo, 0); f.set(respondVo, 0);
} else if (tType.isAssignableFrom(String.class)) {
f.set(respondVo, "");
} else if (tType.isAssignableFrom(List.class)) { } else if (tType.isAssignableFrom(List.class)) {
f.set(respondVo, Collections.EMPTY_LIST); f.set(respondVo, Collections.EMPTY_LIST);
} }
......
package com.ceb.gjjs.mda.util; package com.ceb.gjjs.mda.util;
import com.brilliance.eibs.util.StringUtil;
import com.brilliance.mda.runtime.annotation.Need; import com.brilliance.mda.runtime.annotation.Need;
import com.brilliance.mda.support.jakson.serialize.DecimalLength; import com.brilliance.mda.support.jakson.serialize.DecimalLength;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
...@@ -17,8 +18,14 @@ import java.util.List; ...@@ -17,8 +18,14 @@ import java.util.List;
* @Date 2023/8/8 * @Date 2023/8/8
*/ */
public class ValidatorUtil { public class ValidatorUtil {
public static final ThreadLocal<String> CHECK_ERR_MSG = new ThreadLocal<>();
public static String check(Object obj) { public static String check(Object obj) {
if (StringUtil.isNotEmpty(CHECK_ERR_MSG.get())) {
String s = CHECK_ERR_MSG.get();
CHECK_ERR_MSG.remove();
return "参数校验错误[" + s + "]";
}
Class<?> aClass = obj.getClass(); Class<?> aClass = obj.getClass();
Field[] fields = aClass.getDeclaredFields(); Field[] fields = aClass.getDeclaredFields();
for (Field f : fields) { for (Field f : fields) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment