如何告诉jackson2smilehttpmessageconverter在使用java全局配置进行序列化期间忽略空字段?

r8uurelv  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(265)

我有多个自定义HttpMessageConverter。
json(spring默认使用)-使用jsoninclude.include.non_null
MapJackson2xMLHttpMessageConverter
MapJackson2SmilehttpMessageConverter
是否有类似smileinclude.include.non_null或xmlclude.include.non_null的内容?
在responseobject中,有3个字段
属性名称
来源
列表项
源文件为空,我不希望它出现在响应中。
由于我添加了jsoninclude.include.non_null,源文件不在json响应中,而是以xml和smile格式显示。
以下是所有3种数据格式的响应:
json

{"propertyName":"Mall of ABC","tagline":"Shopping Mall near downtown"}

xml

<SchemaOverview><propertyName>Mall of ABC</propertyName><tagline>Shopping Mall near downtown</tagline><source/></SchemaOverview>

微笑 :) �propertyNameJMall of ABC�taglineZShopping Mall near downtown�source!� 下面是applicationconfig文件

public class ApplicationConfig implements InitializingBean {

public void afterPropertiesSet() throws Exception {
    System.out.println("AppConfig initialized");
}

@Primary
@Bean
public ObjectMapper objectMapper() {
    return new Jackson2ObjectMapperBuilder()
            .serializationInclusion(JsonInclude.Include.NON_NULL) // Ignores null value in Json Response
            .build();
}

// XmlHttpMessageConverter
@Bean
public MappingJackson2XmlHttpMessageConverter
      mappingJackson2XmlHttpMessageConverter() {
  return new MappingJackson2XmlHttpMessageConverter(
      new Jackson2ObjectMapperBuilder()
          .defaultUseWrapper(false)
          .createXmlMapper(true)
          .build()
  );
}

// SmileHttpMessageConverter
@Bean
public MappingJackson2SmileHttpMessageConverter
MappingJackson2SmileHttpMessageConverter() {
  return new MappingJackson2SmileHttpMessageConverter(
      new Jackson2ObjectMapperBuilder()
          .smile()
          .build()
  );
}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题