SpringBoot配置消息转换器

##在@Configuration配置类下添加转换器

@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);
}
上一篇:java使用jackson生成和解析JSON


下一篇:MessagePack Java Jackson Dataformat 不使用 str8 数据类型的