Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the big pit of @ RequestBody with @ Data

Shulou Source: shulou.com Published: 2022-06-03 01:51:20 11月15日 Update

This article mainly explains "how to solve the pit of @ RequestBody with @ Data". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the pit of @ RequestBody with @ Data".

@ RequestBody with @ Data's pit

If you use @ Data to modify an entity class, it's best not to use the same letters in succession, and never capitalize if you use it.

For example, the following User class

@ Datapublic class User {private Integer userId; private String tel; private String QQ;}

If you write like this, the QQ you get with @ RequestBody in the background method will become null

@ PostMapping (value = "/ addPrivGroup") public String addUser (@ RequestBody User user) {return userService.addUser (user);}

If you have to write QQ or qQ, either write getter and setter without @ Data, or add @ JsonProperty (value = "QQ") to the corresponding attribute.

The main reason is that when spring accepts parameters, it does not know whether the assignment method is setQQ or setqQ, which leads to the assignment of null.

@ requestbody,json,Date type conversion problem

@ requestbody receives the problem of mapping json parameters to Date (time and date) type conversion in entity classes

When app transmits Date type data, it has a time difference of 8 hours from that received in the background.

JsonFormat (pattern= "yyyy-MM-dd HH:mm:ss", timezone= "GMT+8") @ DateTimeFormat (pattern= "yyyy-MM-dd HH:mm:ss") private Date departureTime

Timezone mainly solves the "8 hours" problem.

Thank you for your reading. the above is the content of "how to solve @ RequestBody with @ Data". After the study of this article, I believe you have a deeper understanding of how to solve the problem of @ RequestBody with @ Data. The specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Big pits problems types learning content parameters background entities hours that is properties methods time or same not used reasons capitals letters ideas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Apple Shulou Technology NVidia Microsoft