springboot 自定义类型转换 Gson和fastjson

springboot 自定义类型转换

Gson实现

@ConditionalOnMissingBean注解:启动时检测有没有gsonHttpMessageConverter 的bean

如果有,默认类型转换器将不再生效

  1. 引入Maven
<!-- 使用Google的gson -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <exclusions>
      <exclusion>
         <groupId>com.fasterxml.core</groupId>
         <artifactId>jackson-databind</artifactId>
      </exclusion>
   </exclusions>
</dependency>

<dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
</dependency>
  1. 编写配置类
  @Configuration
  public class GsonConfig {
  
      @Bean
      GsonHttpMessageConverter gsonHttpMessageConverter(){
          GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
          GsonBuilder builder = new GsonBuilder();
          builder.setDateFormat("yyyy-MM-dd");
          builder.excludeFieldsWithModifiers(Modifier.PROTECTED);
          Gson gson = builder.create();
          converter.setGson(gson);
          return converter;
      }
  
  }

fastjson实现

  1. 引入Maven
<!-- 使用阿里是fastjson -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <exclusions>
      <exclusion>
         <groupId>com.fasterxml.core</groupId>
         <artifactId>jackson-databind</artifactId>
      </exclusion>
   </exclusions>
</dependency>

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.62</version>
</dependency>
  1. 编写配置类
@Configuration
public class MyFastjsonConfig {

    @Bean
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        config.setDateFormat("yyyy-MM-dd");
        config.setCharset(Charset.forName("UTF-8"));
        config.setSerializerFeatures(
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullStringAsEmpty
        );
        converter.setFastJsonConfig(config);
        return converter;
    }

}

中文乱码解决

server:
  port: 80

spring:
  http:
    encoding:
      force-response: true
补充:fastjson实现方法二
  • 重写WebMvcConfigurer类的configureMessageConverters方法
        //这里是方法体,复制到重写的configureMessageConverters方法中就行了
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        config.setDateFormat("yyyy-MM-dd");
        config.setCharset(Charset.forName("UTF-8"));
        config.setSerializerFeatures(
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullStringAsEmpty
        );
        converter.setFastJsonConfig(config);
        converters.add(converter);
  • 将converter添加到convertersconverters.add(converter);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章