Springboot 默认的 json处理方式 Jackson

  1. 云栖社区>
  2. 博客>
  3. 正文

Springboot 默认的 json处理方式 Jackson

坎布里奇 2019-08-09 18:40:40 浏览228
展开阅读全文

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

网友评论

登录后评论
0/500
评论
坎布里奇
+ 关注