When using JSON to transfer values and @ requestbody annotation, we need to pay attention to some problems
There can only be one @ requestbody annotation in a method
By default, the object marked with @ requestbody must contain all the fields from the foreground
The first one is easy to understand, because the requestbody is the InputStream of the request. This stream will be closed after the annotation is used for the first time, and any subsequent stream will report an error (stream closed)
Second, if the fields from the foreground are not included, an error will be reported: unrecognized fieldxxx, not marked as ignorable, this is because mapping Jackson httpmessage converter requires corresponding fields by default. If there is no field from the front desk, an error will be reported
There are many solutions. You can add a field to receive the value from the foreground. If there are multiple fields, this method is not good (even if one field is useless, the new field is not good)
Or remove useless fields when transferring values from the foreground to the background. This also reduces the size of the network transmission
There are also some methods, which mainly use the JSON annotation provided by Jackson
The @ jsonignore annotation is used to ignore some fields. It can be used in the field or getter method. When it is used in the setter method, it has the same effect as the file. This annotation can only be used when the fields in POJO need to be ignored, which can not meet the current needs
@ jsonignoreproperties (ignoreunknown = true). After this annotation is written on the class, fields that do not exist in the class will be ignored, which can meet the current needs. This annotation can also specify fields to ignore. The usage is as follows:
@JsonIgnoreProperties({ “internalId”, “secretKey” })
The specified field is not serialized and deserialized
Similar Posts:
- [How to Solve] jackson not marked as ignorable
- @Jsonignoreproperties does not work [How to Solve]
- [Solved] com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “FileSi…
- [Solved] Search with xadmin_ Related field got invalid lookup: icontains
- Springboot uses the Datetimeformat (pattern = “yyyy MM DD HH: mm: SS”) annotation to automatically convert the string to date type error
- The second parameter of openfit duplicate mapping and openfit interface method is bindingresult Error
- [Solved] Java.io.EOFException: No content to map to Obje…
- How to Solve Error: Required request body is missing
- [Solved] string(81) “SQLSTATE[HY000]: General error: 1364 Field ‘content’ doesn’t have a default value”
- [Solved] No serializer found for class XXX and no properties discovered to create BeanSerializer