Get the App
SLTechnology News&Howtos  ›  Development  › 

Why is it required in Java that super-large integers are prohibited from returning using the Long type

Shulou Source: shulou.com Published: 2022-06-02 22:13:29 10月03日 Update

This article mainly explains why super-large integers are required in Java to prohibit the use of Long type return. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn why super-large integers are required in Java to prohibit the use of Long type returns.

Error demonstration

Create a Spring Boot project, and then create a new interface that returns a DbScript object, where id is a 19-bit long type value generated by mybatis-plus 's IdWorker.getId (based on the Snowflake algorithm).

@ RestController @ RequestMapping ("/ dbScrip") public class DbScriptController {Logger logger = LoggerFactory.getLogger (DbScriptController.class); @ RequestMapping ("/ info") public DbScript getDbScript () {DbScript dbScript = new DbScript (); / / assign a large integer long script id long id = IdWorker.getId (); dbScript.setId (id); logger.info ("id: {}", id); return dbScript }}

Then start the service and access the interface on the browser, and the result is as follows:

Through the log, we can see that the id transmitted from the back end to the front end is 1304270071757017088, but the front end gets 1304270071757017000, in which the precision loss occurs.

Why did this happen?

Through the development manual, we can know that if the returned value is more than 2 to the power of 53, it will be converted to the Number of JS, and some values may lose precision.

Solution method

So if you encounter such a situation, how to solve it?

Don't panic, you can take the following methods:

If this object is used only in this method, you can change the property directly from Long type to String type.

If this object is used in many places, you can convert the Long type to the String type when serializing.

You can also add a new attribute of type String that is specifically used to transfer such large integers on the front and back ends.

The first method

The first method is relatively simple, directly changing Long id; to String id;, is only applicable to this object and is only used in this method, which is quite limited.

The second method

The second method can add annotations to attributes, and if you use Jackson, you can add @ JsonFormat (shape = JsonFormat.Shape.STRING) or @ JsonSerialize (using = ToStringSerializer.class) annotations.

If there are a lot of situations that need to be modified, it is still a bit troublesome to add them one by one, so is there any good way?

If you are using Jackson, it has a configuration parameter WRITE_NUMBERS_AS_STRINGS, which can force all numbers to be converted to string output. The method is very simple. You only need to configure the parameter: spring.jackson.generator.write_numbers_as_strings=true. The advantage of this method is that it is easy to use and does not need to adjust the code. The disadvantage is that the granularity is so large that all numbers are converted to string output, including the time output in timestamp format.

So is there any other way to just handle Long types and convert them to String types?

Jackson provides this support to customize ObjectMapper, as shown in the following code:

Public class JacksonConfiguration {@ Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer () {return jacksonObjectMapperBuilder-> jacksonObjectMapperBuilder .serial izerByType (Long.class, ToStringSerializer.instance) .serializerByType (Long.TYPE, ToStringSerializer.instance);}}

By defining Jackson2ObjectMapperBuilderCustomizer, customizing the Jackson2ObjectMapperBuilder object, customizing the Long-type data, and using ToStringSerializer to serialize.

The third method

The third method requires an extra attribute, such as using String dbScripId, instead of the previous id.

At this point, I believe you have a deeper understanding of "why super-large integers are required in Java to prohibit the use of Long type returns". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Methods types integers objects properties situations numeric values output code content front ends parameters characters strings sequences losses interfaces numbers comments precision Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Redmi Apple Xiaomi Shulou Technology