maverick
maverick

Reputation: 589

Jackson unrecognized field for class name

Following is how JSON string looks

{
    "employee": {
        "id": "c1654935-2602-4a0d-ad0f-ca1d514a8a5d",
        "name": "smith"
        ...
    }
}

Now i am using ObjectMapper#readValue(jsonAsStr,Employee.class) to convert it to JSON. My Employee class is as follows...

@XmlRootElement(name="employee")
public class Employee implements Serializable {
    private String id;
    private String name;
    ...

    public Employee() {
    }

    @XmlElement(name="id")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id= id;
    }

    @XmlElement(name="name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    ...
}

The exception I am getting is

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: 
Unrecognized field "employee" (class com.abc.Employee), not marked as
ignorable (12 known properties: , "id", "name", ... [truncated]])

I am not able to understand why "employee" is considered as a property. Am i wrong in assuming that only class members are considered as properties?

Upvotes: 6

Views: 5259

Answers (2)

user2475117
user2475117

Reputation: 43

@peeskillet is right. I was looking for a long time about how to use jax annotation to deserialize the json returned from server since I was getting UnrecognizedPropertyException as well.

Adding the following code fixed my problem: mapper.registerModule(new JaxbAnnotationModule());

Follow below the entire code i used: ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JaxbAnnotationModule()); List<PojoTO>response = mapper.readValue(result.readEntity(String.class), mapper.getTypeFactory().constructCollectionType(List.class, PojoTO.class));

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 209102

The problem is that a JSON Object { } maps to a Java class, and the properties in the JSON map to the Java properties. The first { } in your JSON (which you are trying to unmarshal to Employee), has a property employee, which Employee class does not have a property for. That's why you are getting the error. If you were to try and unmarshal only the enclosed { }

{
  "id": "c1654935-2602-4a0d-ad0f-ca1d514a8a5d",
  "name": "smith"
}

it would work as Employee has those properties. If you don't have control over the JSON, then you can configure the ObjectMapper to unwrap the root value

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

But the you might have another problem. The unwrapping is based on the annotation on the Employee class, either @JsonRootName("employee") or @XmlRootElement(name = "employee"). With the latter though, you need to make sure you have JAXB annotation support. For that, you need to have the jackson-module-jaxb-annotations, then register the module

mapper.registerModule(new JaxbAnnotationModule());

This applies for all your JAXB annotations you're using. Without this module, they won't work.

Upvotes: 7

Related Questions