Some attributes of @ RequestBody have not been successfully converted

  • 2021-11-30 00:21:20
  • OfStack

Some attributes of @ RequestBody were not converted successfully

When writing the interface of API adding User in the project, it was discovered that

I set the User parameter to @ ResquestBody, but when I tested it, I found that even if the data of all attributes were passed in, only some attributes of Controller were converted in the received User instance, and other attributes were not converted successfully.

Later, after finding out, it was found that @ ResquestBody only converted the attributes with getter. I added getter to all the attributes, and then it could be converted correctly!

In addition, I found that if it is an boolean attribute, the method of isXX () can also be added

@ RequestBody Some attributes are not worth it

If the attribute name in the entity class received by @ RequestBody is the first letter lowercase and the second letter uppercase, it cannot be normally converted from JSON to instance class attribute

Solutions

Option 1:

Change the name, the second uppercase is changed to lowercase (in actual development, it is inappropriate to change the name so that the name does not conform to the hump nomenclature)

Option 2:

With the @ JsonProperty annotation, specify the name of a method when JSON is converted, and the above entity class will be modified to

 @JsonProperty(value = "pCode")
    private String pCode;

Related articles: