Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What about the loss of data accuracy after the Long type in java is converted to json?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the java Long type into json after the data loss accuracy how to do, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

In the project development, using the architecture of spring boot+mybatis, the database design primary key id is defined as the bigint type, do not notice after using the automatically generated code of mybatis, the type of primary key in pojo is Long. There is no problem with the value taken out from the object list obtained during the query, but after it is converted to json and passed to the front end, the data of id is never the one found in the database.

Design of database table structure

AbumTip class

Query result in database based on foreign key abum_id

The results found by Controller

Chrome browser preview results

You can see that the data of type Long queried by abumId (corresponding table abum_id) and tipId (corresponding table tip_id) is incorrect.

The first method of solution

Regenerate the pojo object, mapping all database types to bigint to String types.

Method two

For using springboot, add the configuration code:

Package com.gj.app.config; 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.Configuration;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.util.List @ EnableWebMvc@Configurationpublic class WebDataConvertConfig extends WebMvcConfigurerAdapter {public void configureMessageConverters (List

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report