我有多个自定义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()
);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!