Where should the JsonSubTypes be read from?

喜欢而已 提交于 2020-01-06 05:25:20

问题


This question is piggy backing off of the other question I had posted as I'm looking for clarity on how the serialization / validation works with regards to the JsonSubTypes

Other question: InvalidSchemaException using JsonSubTypes, not picking up what I defined in the base interface

Let's say I have a base class A

A implements BaseInterface

If I have a sub class B that extends A, but implements it's own interfaces such as AlpahInterface, BetaInterface, CharlieInterface. Herein lies the issue as the validation fails because AlphaInterface only lists e.g. LAMA and so the validation does not know about all of the other types defined in the base BaseInterface.

Subclass B is only implementing whatever is defined by AlphaInterface, ignoring the fact it extends BaseInterface and also ignores the other implemented Interfaces and clearly not paying attention to the fact that B extends A which implements BaseInterface. I could verify this because I added to AlphaInterface all of the subtypes defined in the base interface and things work but I don't want to have to define in every single interface every possible subtype as they are not all relevant to the class that implement them.

I would have thought that either the extended A implementation would be picked up or all of the B implemented interfaces and not just the first one in the listing.

Please someone explain because all of the reading I have done the past two days online have not cleared this up for me. If anyone wants to open a discussion chat that would be great too!

Thanks.

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
  @Type(value = CatTopic.class, name = "CAT"),
  @Type(value = DogTopic.class, name = "DOG"),
  @Type(value = FishTopic.class, name = "FISH"),
  @Type(value = LamaTopic.class, name = "LAMA")
})
public interface BaseInterface {}

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
  @Type(value = LamaTopic.class, name = "LAMA")
})
public interface AlpahInterface extends BaseInterface {}


// Something to extend A
public class B extends A implements AlpahInterface, BetaInterface, CharlieInterface {}

回答1:


I'm not clear as to where this code should be reading in the main listing of JsonSubTypes but I managed to get past my related posting problem. Finally found the way to handle the main issue in my related post. public abstract @interface JsonTypeInfo

What needs to be done is for each of the Classes defined in the JsonSubTypes you must also specify on that Class @JsonTypeName("ClassName") e.g.

@JsonTypeName("CAT")
public class CatTopic extends A implements AlpahInterface, BetaInterface, CharlieInterface {}


来源:https://stackoverflow.com/questions/51557412/where-should-the-jsonsubtypes-be-read-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!