2

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

Devratna
  • 938
  • 1
  • 7
  • 26
  • There is something missing in your info, Spring boot MongoDB does not depend on Jackson by default. So you should have some kind of a converter that is used to convert mongo DBObjects into Java objects using the Jackson. Can you please check and share the converter code as well. – Babl Mar 05 '21 at 14:19
  • 1
    @Babl there is no code related to Jackson, i think spring data by default use Jackson, for ORM – Devratna Mar 05 '21 at 17:32
  • no, spring data mongodb does not have any dependency on Jackson and is not using it by default and never was, you can check that simply by looking here https://github.com/spring-projects/spring-data-mongodb/blob/1.9.x/pom.xml It has its own type mapping system. So there is definitely some "magic" in your project which makes it work – Babl Mar 05 '21 at 18:52

0 Answers0