springboot处理long精度丢失的问题
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;
import java.nio.charset.Charset;
import java.util.List;
/**
* @Classname WebConfig
* @Description TODO
* @Date 2021/8/11 9:06 下午
* @Created by zk
*/
@EnableWebMvc
@Configuration
public class WebMVCConfig extends WebMvcConfigurerAdapter {
/*@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
*//**
* 序列换成json时,将所有的long变成string
* 因为js中得数字类型不能包含所有的java long值
*//*
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
jackson2HttpMessageConverter.setObjectMapper(objectMapper);
converters.add(jackson2HttpMessageConverter);
}*/
@Autowired
public GsonHttpMessageConverter gsonHttpMessageConverter;
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters){
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
stringConverter.setWriteAcceptCharset(false);
converters.add(new ByteArrayHttpMessageConverter());
converters.add(stringConverter);
converters.add(new ResourceHttpMessageConverter(source));
// MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
// jackson2HttpMessageConverter.setObjectMapper();
converters.add(new AllEncompassingFormHttpMessageConverter());
converters.add(gsonHttpMessageConverter);
converters.add(new Jaxb2RootElementHttpMessageConverter());//xml转换
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer){
configurer.favorParameter(false).favorParameter(false);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
方法二
long数据能表示的范围比js中number大,在跟前端交互时,这样也就意味着部分数值在js中存不下(变成不准确的值)
@JsonSerialize(using = ToStringSerializer.class)
将date转化为秒
public class DateToLongSerializer extends JsonSerializer<Date> {
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeNumber(date.getTime() / 1000);
}
可以使用
com.fasterxml.jackson.core-jackson-databind包里边
com.fasterxml.jackson.databind.ser.std下类