裝載:https://blog.csdn.net/ht_kasi/article/details/81230234
1.直接改成字符串
2.加注解
//向前端返回時將Long轉成字符串 public class LongJsonSerializer extends JsonSerializer<Long> { @Override public void serialize(Long value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { String text = (value == null ? null : String.valueOf(value)); if (text != null) { jsonGenerator.writeString(text); } } ———————————————— 版權聲明:本文為CSDN博主「ht_kasi」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/ht_kasi/article/details/81230234
//將接收的前端字符串類型轉換成Long類型 public class LongJsonDeserializer extends JsonDeserializer<Long> { private static final Logger logger = LoggerFactory.getLogger(LongJsonDeserializer.class); @Override public Long deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { String value = jsonParser.getText(); try { return value == null ? null : Long.parseLong(value); } catch (NumberFormatException e) { logger.error("數據轉換異常", e); return null; } } } ———————————————— 版權聲明:本文為CSDN博主「ht_kasi」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/ht_kasi/article/details/81230234
字段上加注解
@JsonDeserialize(using = LongJsonDeserializer.class) @JsonSerialize(using = LongJsonSerializer.class) private Long id;