SpringBoot配置消息转换器

it2022-05-05  172

##在@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); }

最新回复(0)