In the Java extension package, there is a package that can describe the currency type, which says that the currency and amount of money can be used in any complex situation, the object structure is as follows:
{ "price": { "amount": 1799.99, "currency": "USD" }}
If you want to reference a money type package, you can use the following package path
org.javamoney:moneta:1.2.1
Update the package, and then you can use the money type in your code
@Builder @getter@tostring@noargsconstructor@allargsconstructor Public class UserInfo { private String name; Private String email; = 1, message = "The youngest is 1 years old." Private Money price ;}
In the unit test, we take this object as the request body, in the WEBAPI to receive, there is a money serialization problem, the following exception
Com.fasterxml.jackson.databind.exc.InvalidDefinitionException:Cannot construct instance of ' Org.javamoney.moneta.Money '
The main reason is that your money object does not find the composition of the serialization, we need to install a third party composition, to help us realize the serialization of the work, Build.gradle add Package reference
Compile (' Org.zalando:jackson-datatype-money:1.0.0-rc2 ')
Then add the Jackson profile and you can add it at the root of the code
@Configuration class jacksonconfig { @Bean public moneymodule moneymodule () { return New moneymodule (). Withmonetaryamount (money::of); }}
Here are a few global annotations
@Configuration labeled on a class, equivalent to using the class as a spring XML configuration file to <beans>
: Configure the Spring container (application context)
@Bean label on the method (the method that returns an instance), equivalent to the spring XML configuration file <bean>
, that is: registering the Bean object
Here are a few notes to note when using @bean annotations:
- @Bean annotations on the method that returns an instance, if the name of the bean is not specified by @bean, the default is the same as the method name of the callout;
- @Bean annotations The default scope is a singleton singleton scope, which can be set as a prototype scope by @scope ("prototype");
- Since @bean's role is to register bean objects, it is entirely possible to register beans with annotations such as @component, @Controller, @Service, @Ripository, and, of course, to configure @componentscan annotations for automatic scanning.
References to @configuration and @bean annotations in this article come from: 52182899
Thank you for reading!
Problems encountered in serialization of Springboot~money types and their solutions