产生问题的原因:
关于这个问题是由于前端JavaScript的number类型的精度限制,如果说Long类型的值超过了number类型的精度范围,就会出现失去进度这样的问题,导致我们查询时数据id匹配不上。
解决方法:
方法一:使用@JsonSerialize注解将Long类型转换为String类型
@Data
public class QuestionSubmitVO implements Serializable {
/**
* id
*/
@JsonSerialize(using = ToStringSerializer.class)
private Long id;
}
方法二:使用@JsonFormat注解将Long类型格式化为String
public class QuestionSubmitVO implements Serializable {
/**
* id
*/
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Long id;
}
方法三:全局配置Jackson将Long类型序列化为String
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
@Configuration
public class JacksonConfig {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
SimpleModule simpleModule = new SimpleModule();
// 将Long类型序列化为String类型
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
return objectMapper;
}
}