怯懦囚禁人的灵魂,希望可以令你感受到自由,强者自救,圣者渡人。——《肖申克的救赎》

我们可以使用SerializerFeature.WriteEnumUsingToString去完成枚举的序列化操作:

例如我这里某对象属性为枚举类型

image-20210809192748365

我们当使用该POJO接收参数时,我们如果手动转换的话比较麻烦

我们配置如下的后就可以直接传入枚举常量的名字进行映射

image-20210809192609887

例如我此处

image-20210809192918305

最后传入type=INDEX_SHUFFLE,成功完成映射

image-20210809193053398

完整代码

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.ruben.zsxh.config;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.MultipartConfigElement;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* web配置类
*
* @author <achao1441470436@gmail.com>
* @since 2021/5/18 0018 14:52
*/
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

/**
* @param converters 转换器
* @author <achao1441470436@gmail.com>
* @since 2021/5/21 0021 15:08
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
// 设置允许ContentType
List<MediaType> supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
supportedMediaTypes.add(MediaType.APPLICATION_PDF);
supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
supportedMediaTypes.add(MediaType.APPLICATION_XML);
supportedMediaTypes.add(MediaType.IMAGE_GIF);
supportedMediaTypes.add(MediaType.IMAGE_JPEG);
supportedMediaTypes.add(MediaType.IMAGE_PNG);
supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
supportedMediaTypes.add(MediaType.TEXT_HTML);
supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
supportedMediaTypes.add(MediaType.TEXT_PLAIN);
supportedMediaTypes.add(MediaType.TEXT_XML);
fastJsonConverter.setSupportedMediaTypes(supportedMediaTypes);
FastJsonConfig fjc = new FastJsonConfig();
// 配置序列化策略
// ID_WORKER 生成主键太长导致 js 精度丢失
// JavaScript 无法处理 Java 的长整型 Long 导致精度丢失,具体表现为主键最后两位永远为 0,解决思路: Long 转为 String 返回
fjc.setSerializerFeatures(SerializerFeature.BrowserCompatible);
// 列化枚举值为数据库存储值
fjc.setSerializerFeatures(SerializerFeature.WriteEnumUsingToString);
SerializeConfig serializeConfig = SerializeConfig.globalInstance;
// 设置全局LocalDateTime转换
serializeConfig.put(LocalDateTime.class, (serializer, object, fieldName, fieldType, features) -> Opt.ofNullable(object).ifPresentOrElse(obj -> serializer.out.writeString(((LocalDateTime) obj).format(GlobalTimeResolver.DATE_TIME_FORMATTER)), serializer.out::writeNull));
fjc.setSerializeConfig(serializeConfig);
fastJsonConverter.setFastJsonConfig(fjc);
converters.add(fastJsonConverter);
}
}