title: Java Jackson deserializing with inheritance with builder pattern
body: <p>I have a base class (<code>Foo</code>) with 2 children (<code>A</code> and <code>B</code>). They look like this:</p>
  
  <pre><code>public abstract class Foo {
    private String fooString;
    public Foo(String fooString) {
      this.fooString = fooString;
    }
    //getter
  }
  
  @JsonDeserialize(builder = A.ABuilder.class)
  public class A extends Foo {
    private int amount;
    public A(String fooString, int amount) {
      super(fooString);
      this.amount = amount;
    }
    //getter
  
    @JsonPOJOBuilder
    public static class ABuilder {
      private String fooString;
      private int amount;
  
      public ABuilder withFooString(final String fooString) {
          this.fooString = fooString;
          return this;
      }
  
      public ABuilder withAmount(final int amount) {
          this.amount = amount;
          return this;
      }
  
      public A build() {
        return new A(fooString, amount);
      }
    }
  }
  
  @JsonDeserialize(builder = B.BBuilder.class)
  public class B extends Foo {
    private String type;
    public B(String fooString, String type) {
      super(fooString);
      this.type = type;
    }
    //getter
  
    @JsonPOJOBuilder
    public static class BBuilder {
      private String fooString;
      private String type;
  
      public BBuilder withFooString(final String fooString) {
          this.fooString = fooString;
          return this;
      }
  
      public BBuilder withType(final String type) {
          this.type = type;
          return this;
      }
  
      public B build() {
        return new B(fooString, type);
      }
    }
  }
  </code></pre>
  
  <p>In my controller I have this endpoint:</p>
  
  <pre><code>@PutMapping
  private ResponseEntity&lt;Foo&gt; doSomething(@RequestBody Foo dto) {
      //stuff
  }
  </code></pre>
  
  <p>But whenever I try to send over my json payload:</p>
  
  <pre><code>{
     "fooString":"test",
     "amount":1
  }
  </code></pre>
  
  <p>I get the error:</p>
  
  <pre><code>com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.test.Foo` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
   at [Source: (String)"{"fooString":"test","amount":1}; line: 1, column: 1]
      at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
      at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1451)
      at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1027)
      at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:265)
      at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
      at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
      at AbstractJackson.main(AbstractJackson.java:11)
  </code></pre>
  
  <p>How do I get jackson to deserialize the json into the proper child class? What am I doing wrong?</p>