Problems encountered in serialization of Springboot~money types and their solutions

Source: Internet
Author: User

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:

    1. @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;
    2. @Bean annotations The default scope is a singleton singleton scope, which can be set as a prototype scope by @scope ("prototype");
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.