美文网首页
2018-09-04 springboot mybatis返回值

2018-09-04 springboot mybatis返回值

作者: HelloWide | 来源:发表于2018-09-04 15:02 被阅读0次

spring controller会将放回参数中null值默认不放回,可参考如下设置,注意如果application.properties中已设置#spring.jackson.default-property-inclusion=NON_NULL,则函数配置不生效;

```

/**

* @Description 控制返回的值为null的情况替换为空字符串/NULL

* @Author 

* @Date 2018/8/29 17:19

**/

@Configuration

public class ResultConfig {

    @Bean

@Primary

    @ConditionalOnMissingBean(ObjectMapper.class)

    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {

        ObjectMapper objectMapper = builder.createXmlMapper(false).build();

        objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer() {

            @Override

            public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {

//                jsonGenerator.writeString("");//替换字符串

                jsonGenerator.writeNull();//以NUll替代

            }

        });

        return objectMapper;

    }

}

```

相关文章

网友评论

      本文标题:2018-09-04 springboot mybatis返回值

      本文链接:https://www.haomeiwen.com/subject/shwgwftx.html