概述:Spring Boot如何配置全局JSON序列化
j
如何配置全局JSON序列化
- 起因知识储备两种全局序列化方式
- 1.通过`@JsonComponent`2.WebMvcConfigurationSupport配置
起因
当我们在写web接口时,常以JSON格式返回,但是对于一些日期、枚举之类的处理通常需要加@JsonFormat
和@JsonValue
、@JsonSerialize
之类的注解进行特定的序列化处理。那么对于公用的序列化处理该如何实现呢?
知识储备
了解@JsonFormat
和@JsonValue
、@JsonSerialize
之类的注解,方便进行比较 了解Jackson序列化 了解@EnableWebMvc
的作用,点击这里查看
两种全局序列化方式
Spring Boot默认采用Jackson进行序列化,所以提供的两种全局序列化方式都是针对于Jackson,对于fastJson方式下文中也会顺便提一下。本文以对枚举类的全局序列化来举例。
1.通过@JsonComponent
首先话不多说先看个例子。
定义枚举类
public enum CurrCode implements BaseEnum { RMB("156", "rmb"), USD("840", "usd"); private String code; private String desc; CurrCode(String code, String desc) { this.code = code; this.desc = desc; } @JsonCreator public static CurrCode of(@JsonProperty("code") String c) { for (CurrCode value : CurrCode.values()) { if (value.getCode().equals(c)) { return value; } } return null; } public String getCode() { return code; } public String getDesc() { return desc; } @Override public String toString() { return "CurrCode{" + "code='" + code + '\'' + ", desc='" + desc + '\'' + '}'; } @Override public String getValue() { return getCode(); } }
定义配置类
@JsonComponent public class JacksonComponent { public static class CurrCodeJsonSerializer extends JsonSerializer<CurrCode> { @Override public void serialize(CurrCode currCode, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeString(currCode.getCode()); } } }
分析
查看@JsonComponent
源码
@Component that provides JsonSerializer,JsonDeserializer or KeyDeserializer implementations to be registered with Jackson when JsonComponentModule is in use. Can be used to annotate implementations directly or a class that contains them as inner-classes.
翻译:@Component,提供JsonSerializer,JsonDeserializer或KeyDeserializer实现,以便在使用JsonComponentModule时向Jackson进行注册。可用于直接注解实现或包含它们作为内部类的类。
@JsonComponent public class CustomerJsonComponent { public static class Serializer extends JsonSerializer<Customer> { // ... } public static class Deserializer extends JsonDeserializer<Customer> { // ... } }
上面源码里给出的例子在@JsonComponent
注解所修饰的类里面添加继承JsonSerializer的内部类,spring就会自动帮助我们将定义的序列化类注册到jackson里去。这个过程使用的仍然是Spring Boot自动配置web mvc,这样静态资源不会失效。
补充:@JsonComponent
注解value用来指定注册到Spring容器里的Bean Name,type用来指定对哪些类进行了序列化或反序列化处理。scope指定对在作用范围。
2.WebMvcConfigurationSupport配置
@Configuration public class JacksonConfig extends WebMvcConfigurationSupport { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = new ObjectMapper(); SimpleModule simpleModule = new SimpleModule(); simpleModule.addSerializer(CurrCode.class, new CurrCodeJsonSerializer()); objectMapper.registerModule(simpleModule); // 配置null值序列化成空字符串 objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeString(""); } }); //这里是fastJSON的配置方式,更多的内容可以查看SerializerFeature // FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); // converter.setFeatures(SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero, // SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteNullListAsEmpty); converter.setObjectMapper(objectMapper); converters.add(converter); } public static class CurrCodeJsonSerializer extends JsonSerializer<CurrCode> { @Override public void serialize(CurrCode currCode, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeObject(currCode.getCode()); } } }
分析
继承WebMvcConfigurationSupport类主要是需要往web mvc配置中添加对于我们要特殊处理类的转换器(HttpMessageConverter的子类),由于我们使用的是Jackson序列化对象所以需要往转换里添加Jackson提供的转换器即MappingJackson2HttpMessageConverter。如果使用fastjson可以使用转换器FastJsonHttpMessageConverter。
补充:
1.WebMvcConfigurationSupport会屏蔽Spring Boot的自动配置。这样会导致自动配置的静态资源路径(classpath:/META/resources/,classpath:/resources/,classpath:/static/,classpath:/public/)无法访问。
2.上面的配置类同样可以实现WebMvcConfigurer,实现同名的configureMessageConverters方法具体同上。但是需要这个配置类起作用需要在配置类上加上@EnableWebMvc
注解。
3.@EnableWebMvc
注解同样会导致自动配置的静态资源路径无法访问。