SpringMVC @RequestBody Error:Unrecognized field, not marked as ignorable

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: