参考地址
接口参数多余报错问题
当JSON数据中包含Java对象未定义的属性时,Jackson框架无法识别这些额外字段,因为它们没有被标记为可忽略,从而引发“Unrecognized field, not marked as ignorable”错误
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized field "sort" (class com.fan.vo.category.CategoryEditReq), not marked as ignorable]
Long型数据前端精度丢失问题
前端接收后端接口中Long类型数据精度丢失问题可能与前端的数据处理方式有关。在JavaScript中,数字类型是使用浮点数来表示的,而浮点数的精度是有限的,可能导致长整型数据的精度丢失
配置Long类型精度丢失后时间格式不正确问题
变成了时间戳
统一解决方案
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 org.springframework.context.annotation.Bean;
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.WebMvcConfigurer;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.removeIf(MappingJackson2HttpMessageConverter.class::isInstance);
converters.add(0, new MappingJackson2HttpMessageConverter(objectMapper()));
}
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
//忽略未知属性
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//日期格式转换
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
//Long类型转String类型
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
mapper.registerModule(simpleModule);
return mapper;
}
}