/**
* mvc配置类
* @author: nss
* @date: 2018/10/17 20:17
*/
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
private final ObjectMapper mapper;
@Autowired
public MvcConfig(ObjectMapper mapper) {
this.mapper = mapper;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//允许出现特殊字符和转义符
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//允许出现单引号
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
//是否格式化输出
mapper.configure(SerializationFeature.INDENT_OUTPUT,true);
//允许对象忽略json中不存在的属性,忽略无法转换的对象 “No serializer found for class com.xxx.xxx”
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,true);
//列换成json时,将所有的long变成string
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
mapper.registerModule(simpleModule);
//为空和null的不序列化
//Todo 别删
//mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
jackson2HttpMessageConverter.setObjectMapper(mapper);
converters.add(jackson2HttpMessageConverter);
super.configureMessageConverters(converters);
}
}
SpringBoot-MvcConfig.java
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...