Springmvc @RequestBody problem: Unrecognized field, not marked as ignorable

Source: Internet
Author: User

There are some issues to be aware of when using JSON to pass values and using @requestbody annotations:

There can be only one @requestbody annotation in a method. By default, the object that @requestbody annotations must contain all the fields that came from the foreground.


The first one is easy to understand because Requestbody is the inputstream of the request, which is closed after the first use of the annotation, followed by an error (stream closed).


If the second article does not contain the field from the foreground, it will make an error:unrecognized field xxx, not marked as ignorable, This is because the Mappingjacksonhttpmessageconverter default requires that the corresponding field be present. If you don't have a field from the front desk, you'll get an error.


There are many solutions, you can add a field to receive this value from the foreground, if there are more than one field, this method is very bad (even if a field, if not, the new field is not good).


Or in front of the table to pass the value of the time, remove the useless fields. This also reduces the size of the network transmission.


There are a few ways to use the JSON annotations provided by Jackson.


@JsonIgnore annotations are used to omit certain fields and can be used on field or Getter methods, as in setter methods, and filed effects. This annotation can only be used in pojo existing fields to ignore the situation, does not meet the current needs.


@JsonIgnoreProperties (Ignoreunknown = True), after the annotation is written on the class, fields that do not exist in the class are ignored to meet the current needs. This annotation can also specify the fields to ignore. Use the following methods:

@JsonIgnoreProperties ({"Internalid", "Secretkey"})

The specified field is not serialized and deserialized.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.