The problem
The js number on the front end is too large. The length of the js number is more than 17 or about 20 bits. When this happens, the Long returned by the back end will be a perfect exception on the front end
For example, the return value of the backend is:
{"id":695065242312409088}
Copy the code
When the front end receives the id, it will only be used for update, and will not be rendered. Therefore, when capturing the ID for update, the back end will be abnormal, saying that the ID cannot be found, and the front end will print the ID value
{"id":695065242312409100}
Copy the code
why
The cause is that the value is too large and exceeds the maximum value of the front-end
> 695065242312409088 > Number.MAX_SAFE_INTEGER
< true
Copy the code
To solve
Most online solutions are front-end conversion operations, but my value has been returned to the front of the time is already wrong, I can also how to operate it, monkey play after a decisive give up.
Solution: Back end conversion to character type
However, there is a problem, the back end is Long, and the shit mountain of code, who dare to change the type, research found that it can do some dirty operation at the time of serialization
Maven fastjson: Maven fastjson
<fastjson.version>1.2.53</fastjson.version>
Copy the code
Then annotate @jsonField on the id attribute of the entity object
@JSONField(serializeUsing = LongToStringSerializer.class)
private Long id = null;
Copy the code
At this point, if you don’t look down, you’ll start spitting at me and saying that you can’t find the LongToStringSerializer class, here it is:
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import java.io.IOException;
import java.lang.reflect.Type;
public class LongToStringSerializer implements ObjectSerializer {
public static final LongToStringSerializer instance = new LongToStringSerializer();
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
throws IOException {
SerializeWriter out = serializer.out;
if (object == null) {
out.writeNull();
return; } String strVal = object.toString(); out.writeString(strVal); }}Copy the code
Then, before we’re done, hahahahahahahahaha, you’ll notice that the default json serialization resolution for SpringBoot is Jackson, which needs to be resolved
There’s two steps to getting rid of this thing
The first step is to start the class implementation implements WebMvcConfigurer
@SpringBootApplication
public class TmsAppApplication implements WebMvcConfigurer {... }Copy the code
The second step is to match the following code, of course, to the launcher class
/** **@BeanInjection fastJsonHttpMessageConvert * /
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(a) {
// 1. Define a convert object to convert the message;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// add fastJson configuration information, such as whether to format the returned JSON data;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3. Add configuration information to convert.
fastConverter.setFastJsonConfig(fastJsonConfig);
return new HttpMessageConverters(fastConverter);
}
@Override
public void configureMessageConverters(List
> converters)
> {
/* * 1, define a convert object to convert the message; * 2, add fastJson configuration information, such as whether to format the returned JSON data; Add convert to converters. * */
// 1. Define a convert object to convert the message;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// add fastJson configuration information, such as whether to format the returned JSON data;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
//3. Add configuration information to convert.
fastConverter.setFastJsonConfig(fastJsonConfig);
// add convert to converters.
converters.add(fastConverter);
}
Copy the code