Commit 974e9efd by s_guodong

1.参数校验提示修改

2.返回前端时null值处理
parent 22dabe67
...@@ -3,6 +3,7 @@ package com.ceb.gjjs.mda.config; ...@@ -3,6 +3,7 @@ package com.ceb.gjjs.mda.config;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -34,6 +35,10 @@ public class JsonConfig { ...@@ -34,6 +35,10 @@ public class JsonConfig {
// 日期格式处理 // 日期格式处理
// objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); // objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
// 处理null值
SerializerProvider serializerProvider = objectMapper.getSerializerProvider();
serializerProvider.setNullValueSerializer(NullValueSerializer.instance);
return objectMapper; return objectMapper;
} }
......
package com.ceb.gjjs.mda.config;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
/**
* @Description
* @Author s_guodong
* @Date 2023/11/10
*/
public class NullValueSerializer extends JsonSerializer<Object> {
public static final NullValueSerializer instance = new NullValueSerializer();
@Override
public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeObject("");
}
}
...@@ -13,6 +13,7 @@ import java.lang.reflect.ParameterizedType; ...@@ -13,6 +13,7 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
...@@ -109,6 +110,7 @@ public class ReflectUtil { ...@@ -109,6 +110,7 @@ public class ReflectUtil {
declaredField.setAccessible(true); declaredField.setAccessible(true);
Object o = declaredField.get(baseVo); Object o = declaredField.get(baseVo);
if (o == null) { if (o == null) {
setEmptyValue(f, respondVo);
continue; continue;
} }
Class<?> type = declaredField.getType(); Class<?> type = declaredField.getType();
...@@ -203,4 +205,24 @@ public class ReflectUtil { ...@@ -203,4 +205,24 @@ public class ReflectUtil {
} }
/**
* 值为null时设置空值
*
* @param f
* @param respondVo
*/
private static void setEmptyValue(Field f, Object respondVo) throws IllegalAccessException {
Class tType = f.getType();
if (tType.isAssignableFrom(BigDecimal.class)) {
f.set(respondVo, new BigDecimal("0"));
} else if (tType.isAssignableFrom(Double.TYPE) || tType.isAssignableFrom(Double.class)) {
f.set(respondVo, 0D);
} else if (tType.isAssignableFrom(Integer.TYPE) || tType.isAssignableFrom(Integer.class)) {
f.set(respondVo, 0);
} else if (tType.isAssignableFrom(List.class)) {
f.set(respondVo, Collections.EMPTY_LIST);
}
}
} }
...@@ -38,13 +38,13 @@ public class ValidatorUtil { ...@@ -38,13 +38,13 @@ public class ValidatorUtil {
int min = length.min(); int min = length.min();
String stringValue = (String) o; String stringValue = (String) o;
if (stringValue.length() > max || stringValue.length() < min) { if (stringValue.length() > max || stringValue.length() < min) {
return "参数校验错误[" + f.getName() + ":" + message + "]"; return "参数校验错误[" + message + "]";
} }
} else if (type.isAssignableFrom(BigDecimal.class)) { } else if (type.isAssignableFrom(BigDecimal.class)) {
String bigDecimal = ((BigDecimal) o).toPlainString(); String bigDecimal = ((BigDecimal) o).toPlainString();
String replace = bigDecimal.replace(".", ""); String replace = bigDecimal.replace(".", "");
if (replace.length() > max) { if (replace.length() > max) {
return "参数校验错误[" + f.getName() + ":" + message + "]"; return "参数校验错误[" + message + "]";
} }
if (f.isAnnotationPresent(DecimalLength.class)) { if (f.isAnnotationPresent(DecimalLength.class)) {
DecimalLength annotation = f.getAnnotation(DecimalLength.class); DecimalLength annotation = f.getAnnotation(DecimalLength.class);
...@@ -52,7 +52,7 @@ public class ValidatorUtil { ...@@ -52,7 +52,7 @@ public class ValidatorUtil {
if (i > -1) { if (i > -1) {
String substring = bigDecimal.substring(i + 1); String substring = bigDecimal.substring(i + 1);
if (substring.length() > annotation.value()) { if (substring.length() > annotation.value()) {
return "参数校验错误[" + f.getName() + ":小数点最大" + annotation.value() + "]"; return "参数校验错误[" + annotation.message() + "]";
} }
} }
} }
...@@ -62,28 +62,31 @@ public class ValidatorUtil { ...@@ -62,28 +62,31 @@ public class ValidatorUtil {
} }
if (f.isAnnotationPresent(NotEmpty.class) && type.isAssignableFrom(String.class)) { if (f.isAnnotationPresent(NotEmpty.class) && type.isAssignableFrom(String.class)) {
NotEmpty annotation = f.getAnnotation(NotEmpty.class);
if (o == null || StringUtils.isBlank((String) o)) { if (o == null || StringUtils.isBlank((String) o)) {
return "参数校验错误[" + f.getName() + ":不能为空]"; return "参数校验错误[" + annotation.message() + "]";
} }
} }
if (f.isAnnotationPresent(NotNull.class) && !type.isAssignableFrom(String.class)) { if (f.isAnnotationPresent(NotNull.class) && !type.isAssignableFrom(String.class)) {
NotNull annotation = f.getAnnotation(NotNull.class);
if (o == null) { if (o == null) {
return "参数校验错误[" + f.getName() + ":不能为null]"; return "参数校验错误[" + annotation.message() + "]";
} }
} }
if (f.isAnnotationPresent(Need.class)) { if (f.isAnnotationPresent(Need.class)) {
Need annotation = f.getAnnotation(Need.class);
if (o == null) { if (o == null) {
return "参数校验错误[" + f.getName() + ":为必需参数]"; return "参数校验错误[" + annotation.message() + "]";
} }
if (type.isAssignableFrom(String.class) && StringUtils.isBlank((String) o)) { if (type.isAssignableFrom(String.class) && StringUtils.isBlank((String) o)) {
return "参数校验错误[" + f.getName() + ":为必需参数,不能为空]"; return "参数校验错误[" + annotation.message() + "]";
} }
if (ReflectUtil.isPrimate(type)) { if (ReflectUtil.isPrimate(type)) {
} else if ("java.util.List".equals(type.getName())) { } else if ("java.util.List".equals(type.getName())) {
List list = (List) o; List list = (List) o;
if (list.size() == 0) { if (list.size() == 0) {
return "参数校验错误[" + f.getName() + ":元素个数不能为0]"; return "参数校验错误[" + annotation.message() + "]";
} }
// 获取list的泛型 // 获取list的泛型
Class tType = ReflectUtil.getTType(f); Class tType = ReflectUtil.getTType(f);
......
...@@ -12,5 +12,5 @@ public @interface Need { ...@@ -12,5 +12,5 @@ public @interface Need {
boolean value() default true; boolean value() default true;
String message() default "不能为空"; String message() default "必填项不能为空";
} }
...@@ -6,6 +6,8 @@ import java.lang.annotation.*; ...@@ -6,6 +6,8 @@ import java.lang.annotation.*;
@Target(ElementType.FIELD) @Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface DecimalLength { public @interface DecimalLength {
public int value() default 0; int value() default 0;
String message() default "";
} }
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