前端處理后端的Long型數據精度丟失的問題
1、問題描述
- 前端提交參數
{id: "1681224986113204200", status: 0}
- 后端處理
/**
* 根據員工id修改員工賬號
*
* @param employee
* @return
*/
@PutMapping()
public R<String> status(HttpServletRequest request, @RequestBody Employee employee) {
log.info("員工狀態(tài)的設置參數:{}", employee);
employee.setUpdateTime(LocalDateTime.now());
Long empId = (Long) request.getSession().getAttribute("EmployeeId");
employee.setUpdateUser(empId);
employeeService.updateById(employee);
return R.success("員工狀態(tài)修改成功");
}
前端js對long型數據進行處理時丟失精度(只能保證16位),導致提交的id和數據庫中的id不一致。
2、 問題實現
如何解決這個問題?
我們可以在服務端給頁面響應json數據時進行處理,將long型數據統(tǒng)一轉為String字符串
3、具體代碼實現–對象轉換器Jackson0bjectMapper
具體實現步驟:
- 提供對象轉換器Jackson0bjectMapper,基于Jackson進行Java對象到json數據的轉換(直接復制下面代碼到項目中使用)
package com.mannor.reggie_take_out.common;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
/**
* 對象映射器:基于jackson將Java對象轉為json,或者將json轉為Java對象
* 將JSON解析為Java對象的過程稱為 [從JSON反序列化Java對象]
* 從Java對象生成JSON的過程稱為 [序列化Java對象到JSON]
*/
public class JacksonObjectMapper extends ObjectMapper {
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
public JacksonObjectMapper() {
super();
//收到未知屬性時不報異常
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
//反序列化時,屬性不存在的兼容處理
this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
SimpleModule simpleModule = new SimpleModule()
.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))
.addSerializer(BigInteger.class, ToStringSerializer.instance)
.addSerializer(Long.class, ToStringSerializer.instance)
.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
//注冊功能模塊 例如,可以添加自定義序列化器和反序列化器
this.registerModule(simpleModule);
}
}
此工具不僅可以對數據進行json處理,還對時間進行了格式化處理,讓前端處理更方便。文章來源:http://www.zghlxwxcb.cn/news/detail-599668.html
- 在WebMvcConfig配置類中擴展Spring mvc的消息轉換器,在此消息轉換器中使用提供的對象轉換器進行Java對象到json數據的轉換
package com.mannor.reggie_take_out.config;
import com.mannor.reggie_take_out.common.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.util.List;
@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
/**
* 擴展MVC框架的消息轉換器
* @param converters
*/
@Override
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
log.info("擴展到額消息轉換器.....");
//創(chuàng)建消息轉換器對象
MappingJackson2HttpMessageConverter messageConverter=new MappingJackson2HttpMessageConverter();
//設置轉換器對象,底層使用jackson對象將java對象轉為json
messageConverter.setObjectMapper(new JacksonObjectMapper());
//將上面的消息轉換器對象追加帶mvc框架的轉換器集合中
converters.add(0,messageConverter);//為0是設置優(yōu)先使用我們定義的此消息轉換器
super.extendMessageConverters(converters);
}
}
自此,轉換就已經生效了,重啟服務即可測試。文章來源地址http://www.zghlxwxcb.cn/news/detail-599668.html
到了這里,關于前端處理后端傳來的Long型數據精度丟失的問題--對象轉換器Jackson0bjectMapper的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!