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

How to deal with the problem that some attributes of @ RequestBody are not successfully converted?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to deal with the problem that some attributes of @RequestBody have not been successfully converted". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to deal with the problem that some attributes of @RequestBody have not been successfully converted"!

@RequestBody partial attributes were not converted successfully

Found in the project when writing the API interface for adding User

I set the User parameter to @ResquestBody, but when testing, I found that even if all the attributes are passed in, only some attributes in the received User instance are converted, and other attributes are not converted successfully.

Later, after investigation, it was found that @ResquestBody only converted attributes with getters. I added getters to all attributes, and then I could convert them correctly!

In addition, I found that if it is boolean attribute, you can add isXX() method, but also

@RequestBody Some attribute values are not available

@RequestBody The attribute name in the entity class received by @RequestBody cannot be converted from JSON to instance class attribute normally if the first letter is lowercase and the second letter is capitalized.

solutions

Option 1:

Change the name, change the second capital to lowercase on the line (in actual development, change the name so that the name does not conform to the hump naming method is not appropriate)

Option 2:

Add @JsonProperty annotation, specify the name of a method when JSON conversion, the above entity class will be modified to

@JsonProperty(value = "pCode") private String pCode; At this point, I believe that everyone has a deeper understanding of "how to deal with the problem that some attributes of @RequestBody have not been successfully converted". Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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