Home > Back-end >  Jackson Databind error when reading in a POST method
Jackson Databind error when reading in a POST method

Time:11-08

I am trying to create a POST endpoint which receives a ClassB. This class is a subtype of an abstract class ClassA, and there is another ClassC which is a subclass from ClassA. This is like that, because ClassB has a collection of ClassA, so ClassB can contain multiple ClassB or ClassC.

The problem is that I always get a Jackson Databind error while processing the request:

An unknown error occurred while processing the request.: com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class ClassB]: missing type id property 'type'
at [Source: (io.undertow.servlet.spec.ServletInputStreamImpl); line: 67, column: 1]
    at com.fasterxml.jackson.databind.exc.InvalidTypeIdException.from(InvalidTypeIdException.java:43)
    at com.fasterxml.jackson.databind.DeserializationContext.missingTypeIdException(DeserializationContext.java:2083)
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingTypeId(DeserializationContext.java:1596)
    at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._handleMissingTypeId(TypeDeserializerBase.java:307)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedUsingDefaultImpl(AsPropertyTypeDeserializer.java:185)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:119)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:1292)

I have the abstract ClassA as follows:

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = ClassB.class, name = "classb"),
        @JsonSubTypes.Type(value = ClassC.class, name = "classc")})
public abstract class ClassA {

    public ClassA () {
    }
}

The ClassB:

@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@JsonTypeName("classb")
public class ClassB extends ClassA implements Serializable {
  ...
  private Collection<ClassA> myList;
  ...
}

And the ClassC is created like ClassB, but with the "classc" jsonTypeName and without the collection of ClassA elements.

In the pom.xml I have the following dependency:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>

Previously I also had the jsonb dependency in the pom.xml:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency> 

But I commented it out because I thought it could cause some conflict.

In the API, I have the following method:

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createClassB(ClassB classB, @Context UriInfo uriInfo) {
    ...
}

But I always get the same error, as if I would not have added the @JsonTypeName annotation in ClassB and ClassC.

Is there something I am missing?


EDIT: This is the request that I am sending:

{
  ...
  "myList" : [ {
    "type" : "classb",
    ...
  }, {
    "type" : "classb",
    ...
  } ],
  "user" : null
}

CodePudding user response:

try adding type property on the root object...

{
  ...
  "type" : "classb",
  "myList" : [ {
    "type" : "classb",
    ...
  }, {
    "type" : "classb",
    ...
  } ],
  "user" : null
}

CodePudding user response:

The error says that Jackson cannot resolve type property. You should send it along with the object. It means, that when sending ClassB from the client, you have also to add type: "classb" in the object. Then, the Jackson would know which subclass of ClassA to create.

  • Related