fastJson根据String类型枚举值返回数据返回枚举其

因alibaba阿里巴巴kai’fa’shou开发手册规定

【强制】二方库里可以定义枚举类型,参数可以使用枚举类型,但是接口返回值不允许使用枚

举类型或者包含枚举类型的 POJO 对象。

所以就用这个方式返回枚举中的其他方法

首先需要添加一个接口给到枚举类实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
typescript复制代码import com.common.utils.i18n.I18nUtils;
import com.fasterxml.jackson.annotation.JsonProperty;

public interface BaseEnum {

/**
* 获取I8N国际化key
*
* @return code
*/
String key();

/**
* 获取存入数据库的值
*
* @return value
*/
String value();

/**
* 获取I18N国际化信息
*
* @return 国际化信息
*/
@JsonProperty("Description")
default String getDescription() {
return I18nUtils.getEnumMessage(key());
}

@JsonProperty("Value")
default String getValue() {
return value();
}
}

然后创建一个注解提供给到序列化识别并获取参数

1
2
3
4
5
6
7
8
9
10
11
12
java复制代码import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FastJsonEnum {
Class<? extends Enum> enumClass();
String methodName();
}

最后就是FastJson的序列化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
ini复制代码import com.alibaba.fastjson.serializer.AfterFilter;
import com.common.base.code.BaseExceptionCode;
import com.common.base.enums.BaseEnum;
import com.common.base.excetion.BaseException;
import com.common.comment.FastJsonEnum;
import com.common.utils.logger.LoggerUtils;
import com.common.utils.verification.ValidateUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.slf4j.Logger;

/**
* fastJson转换枚举值到json内
*/
public class FastJsonAfterFilter extends AfterFilter {

private Field field = null;

private Logger logger = LoggerUtils.logger(getClass());

@Override
public void writeAfter(Object o) {
Field[] fields = o.getClass().getDeclaredFields();
for (Field field : fields) {
FastJsonEnum enumResult = field.getAnnotation(FastJsonEnum.class);
if (ValidateUtils.isEmpty(enumResult)) {
continue;
}
BaseEnum[] enumInstances = (BaseEnum[]) enumResult.enumClass().getEnumConstants();
try {
String name = field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
Method method = o.getClass().getMethod("get" + name);
Object enumStr = method.invoke(o);
for (BaseEnum enumInstance : enumInstances) {
String desc = enumInstance.getDescription();
String label = enumInstance.getValue();
if (String.valueOf(enumStr).equals(label)) {
super.writeKeyValue(enumResult.methodName(), desc);
}
}
} catch (Exception e) {
logger.error("fastJson 转义注解失败,失败异常为 e:{}",e);
throw new BaseException(BaseExceptionCode.BASE_EXCETION_CODE);
}
}
}
}

然后将此方法设置在FastJsonConfig

1
2
3
4
ini复制代码    FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
fastJsonConfig.setCharset(Charset.forName("UTF-8"));
fastJsonConfig.setSerializeFilters(new FastJsonAfterFilter());

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%