zoukankan      html  css  js  c++  java
  • SpringBoot自定义序列化的使用方式--WebMvcConfigurationSupport

    场景及需求: 项目接入了SpringBoot开发,现在需求是服务端接口返回的字段如果为空,那么自动转为空字符串。

    例如:
    [
         {
             "id": 1,
             "name": null
         },
         {
             "id": 2,
             "name": "xiaohong"
         }
    ]

    如上,格式化后的返回内容应该为:
    [
         {
             "id": 1,
             "name": ""
         },
         {
             "id": 2,
             "name": "xiaohong"
         }
    ]

    这里直接给出解决方案代码,这里支持FastJson和Jackson配置序列化的方式:

    @Configuration
    public class WebCatMvcConfiguration extends WebMvcConfigurationSupport {

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    ObjectMapper objectMapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addSerializer(new ToStringSerializer(Long.TYPE));
    module.addSerializer(new ToStringSerializer(Long.class));
    module.addSerializer(new ToStringSerializer(BigInteger.class));
    objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
    @Override
    public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    jsonGenerator.writeString("");
    }
    });
    objectMapper.registerModule(module);
    converter.setObjectMapper(objectMapper);

    //这里是fastJSON的配置方式,更多的内容可以查看SerializerFeature
    // FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    // converter.setFeatures(SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero,
    // SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteNullListAsEmpty);
    converters.add(converter);
    }
    }



    最后我们也可以了解一下:WebMvcConfigurationSupport类
    下面是它的官方文档描述:

    public class WebMvcConfigurationSupport
    extends Object
    implements ApplicationContextAware, ServletContextAware
    This is the main class providing the configuration behind the MVC Java config. It is typically imported by adding @EnableWebMvc to an application@Configuration class. An alternative more advanced option is to extend directly from this class and override methods as necessary remembering to add@Configuration to the subclass and @Bean to overridden @Bean methods. For more details see the Javadoc of @EnableWebMvc.

    This class registers the following HandlerMappings:

     

    Registers these HandlerAdapters:

    Registers a HandlerExceptionResolverComposite with this chain of exception resolvers:

    Registers an AntPathMatcher and a UrlPathHelper to be used by:

    Note that those beans can be configured with a PathMatchConfigurer.

    Both the RequestMappingHandlerAdapter and the ExceptionHandlerExceptionResolver are configured with default instances of the following by default:

     

  • 相关阅读:
    jdk.jre.jvm 三者之间的关系?
    SQL7
    SQL6
    sql题
    sql题
    sql
    简说SQL题
    递归算法
    JAVA的简单描述
    第一篇技术博客
  • 原文地址:https://www.cnblogs.com/wang-meng/p/9163875.html
Copyright © 2011-2022 走看看