4

I have an abstract class

    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "_type", visible = true, include = As.EXISTING_PROPERTY)
    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
    @JsonSubTypes({
       @Type(value = B.class, name = "B"),
       ...
    })
    public abstract class A {
      ...
    }

and one of subclasses

@DiscriminatorValue("B")
@JsonTypeName("B")
public class B extends A {
  ...
}

When I want to create one entity I need to specify "_type" property. Which is perfectly good. But when I want a list of A-s, I can't get property "_type" specified in JSON response.

I've tried multiple solutions but cannot find the right way to do it. I've tried to add "_type" property with transactional, tried to add "include = JsonTypeInfo.As.PROPERTY". But still nothing.

Of course for everything I have only one controller for class A.

Can anyone tell me how to get "_type" in get response and other responses?

tnx

Matija Župančić
  • 1,080
  • 2
  • 11
  • 21

1 Answers1

3

It looks like a bug, not sure where though. Anyway, I have found a workaround for it today.

@GetMapping
public Page<MyObject> findAll(Pageable pageable) {
   Page<MyObject> page = ...;
   return new PageImpl<>(page.getContent(), pageable, page.getTotalElements()) {};
}

Note that the new instance of anonymous class extending PageImpl is being returned here (not a new instance of PageImpl) to prevent type erasure.

Maciej Dobrowolski
  • 11,561
  • 5
  • 45
  • 67