Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

335
Views
Why does Jackson ignore in-class annotations but respect mix-in annotations?

I use the swagger-codegen-maven-plugin version 2.2.2 to generate Java classes for a REST API. The API uses polymorphism and Swagger provides corresponding annotations:

@JsonTypeInfo(
  use = JsonTypeInfo.Id.NAME, 
  include = JsonTypeInfo.As.PROPERTY, 
  property = "type", 
  visible = true )
@JsonSubTypes({
  @Type(value = Cat.class, name = "Cat"),
  @Type(value = Dog.class, name = "Dog") })

public class Pet{

  @JsonProperty("type")
  private String type = null;

  // getter and setters

}

Now I use a Jackson ObjectMapper to parse the incoming JSON string. I use the Jackson that comes with the 1.5.2.RELEASE of spring-boot-starter-parent:

ObjectMapper mapper = new ObjectMapper();
String body = "{ \"pet\": { \"type\": \"Dog\" } }";
Content content = mapper.readValue(body, Content.Class);

However, this fails. Jackson ignores the annotations and maps everything to Pet instead of Dog:

Dog dog = (Dog) content.getPet(); // -> ClassCastException

As a workaround, I added mix-in annotations that do nothing more than the original annotations. The only difference is that they reside in a separate class:

@JsonTypeInfo(
  use = JsonTypeInfo.Id.NAME, 
  include = JsonTypeInfo.As.PROPERTY, 
  property = "type", 
  visible = true)
@JsonSubTypes({
  @Type(value = Cat.class, name = "Cat"), 
  @Type(value = Dog.class, name = "Dog")})

public class PetAnnotations {
}

Now this works:

// like above
mapper.addMixInAnnotations(Pet.class, PetAnnotations.class);
Content content = mapper.readValue(body, Content.Class);
Dog dog = (Dog) content.getPet(); // -> works!

Any ideas why Jackson ignores the in-class annotations but respects the annotations I explicitly hand in as mix-ins?

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!