question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UnrecognizedPropertyException when using jackson inheritance

See original GitHub issue

When I am using jackson inheritance thru @JsonTypeInfo the common field for whole inheritance model, which is defined inside annotation leads to UnrecognizedPropertyException during serialization. To avoid this, I have to explicitly create setProperty() method in POJO what I do not want to do, due to I then just have 2 copies of the field: one from annotation and second in my POJO class, sure I can ignore one of them but then this inheritance model looks weird. As a workaround I can annotate my POJO with @JsonIgnoreProperties(ignoreUnknown = true), but in some cases I need to have ignoreUnknown = false and this workaround will not work.

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true)
@JsonSubTypes({
        @JsonSubTypes.Type(value = TypeOne.class,
            name = "ONE"),
        @JsonSubTypes.Type(value = TypeTwo.class,
            name = "TWO")
    })
public class AbstractType{}

public class TypeOne
    extends AbstractType{}

public class TypeTwo
    extends AbstractType{}  
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "type" (class TypeOne), not marked as ignorable (0 known properties: ])

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Sep 19, 2016

This is because for some reason you added visible="true" in there – that exposes “type” property to deserializer. Perhaps you just copy-pasted @JsonTypeInfo from an existing POJO?

Anyway, since visible defaults to false, you can just remove it from the declaration (or set to false if you prefer explicitness).

0reactions
cowtowncodercommented, Sep 20, 2016

Ok no problem. Glad it works. I understand that property name itself is ambiguous and could lead to assumption that visibility would refer to inclusion (or not) in JSON serialization.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Jackson with JSON: Unrecognized field, not marked as ...
Can someone help please? org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "wrapper" (Class Wrapper), not marked as ignorable ...
Read more >
Inheritance in Jackson | Baeldung
This tutorial will demonstrate how to handle inclusion of subtype metadata and ignoring properties inherited from superclasses with Jackson.
Read more >
UnrecognizedPropertyException in Java - GeeksforGeeks
We create a json file with a new property added as phoneNumber. However, this property is not present in the Employee class. When...
Read more >
Jackson JSON Parsing Error - Java Code Geeks - 2022
UnrecognizedPropertyException : Unrecognized field XXX, not marked as ignorable” error comes when you try to parse JSON to a Java object which ...
Read more >
UnrecognizedPropertyException (jackson-databind 2.0.0-RC1 ...
Convenience method for accessing logical property name that could not be mapped. Methods inherited from class com.fasterxml.jackson.databind.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found