Springboot 默认的json处理方式 Jackson
Springboot 处理返回参数的设置问题,接上篇SpringBoot 整合 fastjson
配置文件
package com.summer.isnow.config;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import java.io.IOException;
/**
* @author liudongting
* @date 2019/8/9 10:29
*/
@Configuration
public class JacksonConfig {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString("");
}
});
return objectMapper;
}
}
实体类
@JsonView(value = View.Base.class )
private Map<String,String> map;
@JsonView(value = View.Base.class )
private String [] ss;
@JsonView(value = View.Base.class )
private int [] intDemo;
@JsonView(value = View.Base.class )
private Integer b =null;
@JsonView(value = View.Base.class )
private boolean bbbb ;
@JsonView(value = View.Base.class )
private List<String> dd;
返回结果
源码地址:
- 包含Springboot 结合 fastjson、jackson处理返回值的不同方法,以及使用@JsonView注解返回自己需要的字段
- 一些工具包,包括微信登录、推送、验证内容、图片上传等功能
github