springboot自定义转换处理springmvc返回json时各类型为null情况

以前都是直接查库返回实体类,当库字段为null时,返回json时字段为null

1、第一种解决方案

使用@JsonInclude(JsonInclude.Include.NON_NULL)将字段为null的忽略,直接返回有值得参数。

但由于前端想要全部字段,而且得根据类型传空,不能为null,类似(String类型的返回“”,Integer类型返回0)。

网上查询都是直接修改ObjectMapper,设置serializer为null时候直接设置空字符串,但这样不管是string,integer都会被设置为null。所以采用第二种方案

2、第二种解决方案

通过继承BeanSerializerModifier

public class MyBeanSerializerModifier extends BeanSerializerModifier {

    private JsonSerializer _nullArrayJsonSerializer = new MyNullArrayJsonSerializer();

    private JsonSerializer _nullStringJsonSerializer = new MyNullStringJsonSerializer();

    private JsonSerializer _nullIntegerJsonSerializer = new MyNullIntegerJsonSerializer();

    @Override
    public List changeProperties(SerializationConfig config, BeanDescription beanDesc,
                                                     List beanProperties) {
        // 循环所有的beanPropertyWriter
        for (int i = 0; i < beanProperties.size(); i++) {
            BeanPropertyWriter writer = beanProperties.get(i);
            // 判断字段的类型,如果是array,list,set则注册nullSerializer
            if (isArrayType(writer)) {
                //给writer注册一个自己的nullSerializer
                writer.assignNullSerializer(this.defaultNullArrayJsonSerializer());
            }
            if (isStringType(writer)) {
                writer.assignNullSerializer(this.defaultNullStringJsonSerializer());
            }

            if (isIntegerType(writer)) {
                writer.assignNullSerializer(this.defaultNullIntegerJsonSerializer());
            }
        }
        return beanProperties;
    }

    // 判断是什么类型
    protected boolean isArrayType(BeanPropertyWriter writer) {
        Class clazz = writer.getPropertyType();
        return clazz.isArray() || clazz.equals(List.class) || clazz.equals(Set.class);
    }

    protected boolean isStringType(BeanPropertyWriter writer) {
        Class clazz = writer.getPropertyType();
        return clazz.equals(String.class);
    }

    protected boolean isIntegerType(BeanPropertyWriter writer) {
        Class clazz = writer.getPropertyType();
        return clazz.equals(Integer.class) || clazz.equals(int.class);
    }

    protected JsonSerializer defaultNullArrayJsonSerializer() {
        return _nullArrayJsonSerializer;
    }

    protected JsonSerializer defaultNullStringJsonSerializer() {
        return _nullStringJsonSerializer;
    }

    protected JsonSerializer defaultNullIntegerJsonSerializer() {
        return _nullIntegerJsonSerializer;
    }
} 
  

对应处理的类型null

集合Array

public class MyNullArrayJsonSerializer extends JsonSerializer {

    @Override
    public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
        if (value == null) {
            jgen.writeStartArray();
            jgen.writeEndArray();
        } else {
            jgen.writeObject(value);
        }
    }
}
 
  

Integer数字()

public class MyNullIntegerJsonSerializer extends JsonSerializer {

    @Override
    public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
        if (value == null) {
            jgen.writeNumber(0);
        }
    }
} 
  

String字符串

public class MyNullStringJsonSerializer extends JsonSerializer {

    @Override
    public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
        if (value == null) {
            jgen.writeString("");
        }
    }
} 
  

最后将咱们继承了BeanSerializerModifier的MybeanSerializeModifier设置给ObjectMapper做处理,通过@Configuration注解交给spring管理,代码如下

@Configuration
public class ClassJsonConfiguration {

    @Bean
    public MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() {

        final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

        ObjectMapper mapper = converter.getObjectMapper();

        // 为mapper注册一个带有SerializerModifier的Factory,此modifier主要做的事情为:判断序列化类型,根据类型指定为null时的值

        mapper.setSerializerFactory(mapper.getSerializerFactory().withSerializerModifier(new MyBeanSerializerModifier()));

        return converter;
    }

}

由于项目是用的微服务,再定义一个自定义注解提供给各个服务使用,单个项目就上面步骤就可以了。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(ClassJsonConfiguration.class)
@Documented
public @interface EnableClassJsonConfig {
}

已上。也做为自己的记录,也希望对有这方面需要的人有帮助。

参考博客:https://www.cnblogs.com/lic309/p/5048631.html 

你可能感兴趣的:(java,springboot,null,注解)