current position:Home>Java type conversion problem, is there a useful tool?

Java type conversion problem, is there a useful tool?

2022-02-03 01:13:36 CSDN Q & A

The phenomenon and background of the problem

grpc It's transmitted bean Need to be stored in the database , But because grpc adopt proto Generated bean and spring-jpa Stored in the database bean Not a type of bean, But what's inside , data type , All attributes are the same , You need a type conversion , Is there a mature approach to this type conversion , At present, I only use reflection for conversion , Feel relatively troublesome .




Refer to the answer 1:

It came from here bean It's a json The string still has an object of the specified type
If it is json If so, use directly fastsjson transformation
If it is an object of the specified type and the attribute is consistent with the database type, you can use org.springframework.beans.BeanUtils. Under the copyProperties Method copies the data of the source object to the new object




Refer to the answer 2:



Refer to the answer 3:

Gson library

img

Transformation method , You can use one directly Gson object from , Example :

img

Just make sure that json Medium key It can be consistent with the class attribute name




Refer to the answer 4:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030113345305.html

Random recommended