修改bug
This commit is contained in:
@@ -24,7 +24,24 @@ public class JacksonConfig {
|
||||
private static final JsonSerializer<Long> LONG_SERIALIZER = new JsonSerializer<Long>() {
|
||||
@Override
|
||||
public void serialize(Long value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||
gen.writeString(value.toString());
|
||||
if (value == null) {
|
||||
gen.writeNull();
|
||||
} else {
|
||||
gen.writeString(value.toString());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private static final JsonSerializer<Object> NUMBER_TO_STRING_SERIALIZER = new JsonSerializer<Object>() {
|
||||
@Override
|
||||
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||
if (value == null) {
|
||||
gen.writeNull();
|
||||
} else if (value instanceof Number) {
|
||||
gen.writeString(String.valueOf(((Number) value).longValue()));
|
||||
} else {
|
||||
gen.writeString(value.toString());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -38,11 +55,16 @@ public class JacksonConfig {
|
||||
|
||||
@Bean
|
||||
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
|
||||
return builder
|
||||
.serializerByType(Long.class, LONG_SERIALIZER)
|
||||
.serializerByType(long.class, LONG_SERIALIZER)
|
||||
ObjectMapper mapper = builder
|
||||
.deserializerByType(Long.class, LONG_DESERIALIZER)
|
||||
.deserializerByType(long.class, LONG_DESERIALIZER)
|
||||
.build();
|
||||
|
||||
// 使用兼容 Number 类型的序列化器
|
||||
mapper.registerModule(new com.fasterxml.jackson.databind.module.SimpleModule()
|
||||
.addSerializer(Long.class, NUMBER_TO_STRING_SERIALIZER)
|
||||
.addSerializer(long.class, NUMBER_TO_STRING_SERIALIZER));
|
||||
|
||||
return mapper;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user