UnrecognizedPropertyException when using jackson inheritance
See original GitHub issueWhen 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:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 tofalse
, you can just remove it from the declaration (or set tofalse
if you prefer explicitness).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.