Not able to use @JsonTypeInfo for mongoDb entity in spring-boot 2.1.x

Viewed 482

I have existing code that is working fine with spring mongo version 1.9.4.RELEASE. Now migrating it to version 2.2.1.RELEASE, but I'm facing an issue with @JsonTypeInfo.

Following is the sample code:

BaseInterface.java

@JsonTypeInfo(use = Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "ty")
@JsonSubTypes(value = {
    @JsonSubTypes.Type(value = Featured.class, name = "Featured")
})
public interface BaseInterface {
 
  String getType(); 

}

Featured.class

public class Featured implements BaseInterface {

 @JsonProperty("ty")
 private String type;

 public String getType() {
   return type;
 }

 public void setType(String type) {
  this.type = type;
 }
}

Entity1.class

@Data
public class Entity1 {

  @Id
  private String id;

  private BaseInterface base;
  
}

Database Entry

{
   "_id" : ObjectId("59d5e9eae4b0af676ead23ed"),
   "base" : {
              "_class" : "com.test.Featured",
              "type" : "Featured"
            }
}

Exception:

org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate using constructor NO_CONSTRUCTOR with arguments

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [BaseInterface]: Specified class is an interface

0 Answers
Related