Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson Json Deserialisation: Unrecognized field "..." , not marked as ignorable

Tags:

I'm getting the following error and no resolution i found did the trick for me:

Unrecognized field "GaugeDeviceId" (Class GaugeDevice), not marked as ignorable

The problem seems, that the service returns the property names with a leading upper letter, while the class properties begin with a lower letter.

I tried:

  1. changing the propertyNames to first upper letter - same error
  2. adding @JsonProperty("SerialNo") to the property instantiation - same error
  3. adding @JsonProperty("SerialNo") to the corresponding getters - same error
  4. adding @JsonProperty("SerialNo") to the corresponding setters - same error
  5. adding @JsonProperty("SerialNo") to all of them (just for fun) - same error

(note: @JsonProperty("SerialNo") is just an example)

The strange thing is, that annotation: @JsonIgnoreProperties(ignoreUnknown = true) should suppress exactly that error, but it is still triggering...

here the Class: (note: not complete)

@JsonIgnoreProperties(ignoreUnknown = true)
public class GaugeDevice 
{
    private int gaugeDeviceId;
    private Date utcInstallation;
    private String manufacturer;
    private float valueOffset;
    private String serialNo;
    private String comment;
    private int digitCount;
    private int decimalPlaces;

    @JsonProperty("SerialNo")
    public String getSerialNo() {
        return serialNo;
    }

    public void setSerialNo(String serialNo) {
        this.serialNo = serialNo;
    }

    @JsonProperty("Comment")
    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

Where is the way out here? Please help.

edit:

Here is the Client Class: (just a simple test client)

import ccc.android.meterdata.*;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import org.glassfish.jersey.jackson.JacksonFeature;

public class RestClient
{
    private String connectionUrl;
    private javax.ws.rs.client.Client client;

    public RestClient(String baseUrl) {
         client = ClientBuilder.newClient();;
         connectionUrl = baseUrl;
         client.register(JacksonFeature.class); 
    }

    public GaugeDevice GetGaugeDevice(int id){

        String uri = connectionUrl + "/GetGaugeDevice/" + id;
        Invocation.Builder bldr = client.target(uri).request("application/json");
        return bldr.get(GaugeDevice.class);
    }
}

I hope the error has its root here?

like image 691
chile Avatar asked Mar 04 '14 13:03

chile


People also ask

How do I ignore unrecognized fields Jackson?

You can ignore the unrecognized fields by configuring the ObjectMapper class: mapper. configure(DeserializationFeature. FAIL_ON_UNKNOWN_PROPERTIES, false);

What is unrecognized property exception?

If the JSON string consists of properties that cannot be mapped to java class attributes, then we encounter UnrecognizedPropertyException.

How do I ignore JSON fields?

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.

What is the use of @JsonProperty?

@JsonProperty is used to mark non-standard getter/setter method to be used with respect to json property.


2 Answers

I had the same issue and I resolved it by changing the annotation import from:

com.fasterxml.jackson.annotation.JsonIgnoreProperties

to

org.codehaus.jackson.annotate.JsonIgnoreProperties

Didn't have to define any NamingStrategy or ObjectMapper.

like image 135
Christos Avatar answered Oct 05 '22 13:10

Christos


I had the same issue and solved it by changing the annotation import from

com.fasterxml.jackson.annotation.JsonProperty

to

org.codehaus.jackson.annotate.JsonProperty
like image 31
Bilal Avatar answered Oct 05 '22 11:10

Bilal