105

I am using Jackson and I'm having problems, when I try to deserialize an Object I get the following error:

com.fasterxml.jackson.databind.JsonMappingException: 
    Can not construct instance of net.MyAbstractClass, 
    problem: abstract types either need to be mapped to concrete types, 
        have custom deserializer, or be instantiated with additional type information

I am having problems in the attribute:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT, property = "@id")
@JsonSubTypes({ @JsonSubTypes.Type(value = MyAbstractClass.class, name = "MyAbstractClass") })
@ManyToOne
private MyAbstractClass object;

Could anyone help me?

koppor
  • 19,079
  • 15
  • 119
  • 161
Danilo M.
  • 1,422
  • 5
  • 17
  • 31
  • Another discussion on a similar case is done at https://stackoverflow.com/a/32777371/873282. Especially interesting is the comment stating to use `@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "type")`. In that way, the JSON is a bit larger, but it is explicit, which type should be used. – koppor Jun 27 '17 at 10:54

10 Answers10

71

You cannot instantiate an abstract class, Jackson neither. You should give Jackson information on how to instantiate MyAbstractClass with a concrete type.

See this answer on stackoverflow: Jackson JSON library: how to instantiate a class that contains abstract fields

And maybe also see Jackson Polymorphic Deserialization

thomas.mc.work
  • 6,404
  • 2
  • 26
  • 41
jlabedo
  • 1,106
  • 8
  • 8
  • 3
    Thanks for help, I resolved with: `JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "class")` the `property = "class"`, is coming in my JSON informing what child class will be used. – Danilo M. Oct 05 '12 at 20:45
  • 1
    Beware, allowing users to specify which class to use during deserialization might lead to vulnerabilities such as [this](http://fishbowl.pastiche.org/2015/11/09/java_serialization_bug/). – andres.riancho Dec 22 '15 at 16:24
  • 2
    Thanks for the reference: @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class") does the trick. Like the JPA table descriminator value. Thanks. – 99Sono Jul 04 '16 at 15:35
24

I was having this issue using lombok's @Data and @Builder annotations alone, which I replaced with:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder

And it solved my problem.

cigien
  • 57,834
  • 11
  • 73
  • 112
Gabriel Andrade
  • 684
  • 4
  • 8
18

You need to use a concrete class and not an Abstract class while deserializing. if the Abstract class has several implementations then, in that case, you can use it as below-

  @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
    @JsonSubTypes({ 
      @Type(value = Bike.class, name = "bike"), 
      @Type(value = Auto.class, name = "auto"), 
      @Type(value = Car.class, name = "car")
    })
    public abstract class Vehicle {
        // fields, constructors, getters, setters
    }
Asif
  • 465
  • 5
  • 6
  • Worked for me here. Important to mention that `Vehicle` has a field `type`, which will create the correct object depending on the `name` defined of it. – finx Dec 22 '20 at 16:35
  • Does this work with resteasy? To me - not! – Alex Mi Dec 17 '21 at 07:36
6

Your @JsonSubTypes declaration does not make sense: it needs to list implementation (sub-) classes, NOT the class itself (which would be pointless). So you need to modify that entry to list sub-class(es) there are; or use some other mechanism to register sub-classes (SimpleModule has something like addAbstractTypeMapping).

StaxMan
  • 113,358
  • 34
  • 211
  • 239
5

In your concrete example the problem is that you don't use this construct correctly:

@JsonSubTypes({ @JsonSubTypes.Type(value = MyAbstractClass.class, name = "MyAbstractClass") })

@JsonSubTypes.Type should contain the actual non-abstract subtypes of your abstract class.

Therefore if you have:

abstract class Parent and the concrete subclasses

Ch1 extends Parent and Ch2 extends Parent

Then your annotation should look like:

@JsonSubTypes({ 
          @JsonSubTypes.Type(value = Ch1.class, name = "ch1"),
          @JsonSubTypes.Type(value = Ch2.class, name = "ch2")
})

Here name should match the value of your 'discriminator':

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, 
include = JsonTypeInfo.As.WRAPPER_OBJECT, 
property = "type")

in the property field, here it is equal to type. So type will be the key and the value you set in name will be the value.

Therefore, when the json string comes if it has this form:

{
 "type": "ch1",
 "other":"fields"
}

Jackson will automatically convert this to a Ch1 class.

If you send this:

{
 "type": "ch2",
 "other":"fields"
}

You would get a Ch2 instance.

ACV
  • 9,964
  • 5
  • 76
  • 81
4

For me there was no default constructor defined for the POJOs I was trying to use. creating default constructor fixed it.

public class TeamCode {

    @Expose
    private String value;

    public String getValue() {
        return value;
    }

    **public TeamCode() {
    }**

    public TeamCode(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "TeamCode{" +
                "value='" + value + '\'' +
                '}';
    }

    public void setValue(String value) {
        this.value = value;
    }

}
ZIA UR REHMAN
  • 119
  • 12
1

Use @JsonDeserialize(contentAs= ConcreteCLass.class)

Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301
1

Setting a default constructor helped to resolve

0

Use lombok annotation.

@AllArgsConstructor
@NoArgsConstructor
@Builder
@JsonPropertyOrder({"name", "id"})
public class CreationUser {
    @Builder.Default
    @JsonProperty("name")
    String name = "";

    @Builder.Default
    @JsonProperty("id")
    String id = "";
}
0

Using lombok I had the same issue, I have tried this to solve

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder