RestyGWT Polymorphic Encode/Decode issues when using an interface instead of an abstract class

StackOverflow https://stackoverflow.com/questions/19106772

  •  29-06-2022
  •  | 
  •  

문제

According to RestyGWT documentation one must use an abstract super class for this to work, for instance, given:

@JsonSubTypes(@Type(value=PersonImpl.class, name="PersonImpl"))
@JsonTypeInfo(use=Id.NAME, include=As.PROPERTY, property="@class")
public abstract class Person{
    public abstract String getName();
    public abstract void setName(String name);
}

@JsonTypeName("PersonImpl")
public class PersonImpl extends Person{
    private String name;

    @Override
    public final String getName() {
        return name;
    }
    @Override
    public final void setName(String name) {
        this.name = name;
    }
}

If I use the defined encoder/decoder this would work:

Person personInstance = new PersonImpl();
personInstance.setName("TestName");

PersonCodec codec = GWT.create(PersonCodec.class);
JSONValue json = codec.encode(personInstance);

Im trying to do something quite similar but with a small difference, that is, instead of Person being an abstract class I want it to be an Interface:

@JsonSubTypes(@Type(value=PersonImpl.class, name="PersonImpl"))
@JsonTypeInfo(use=Id.NAME, include=As.PROPERTY, property="@class")
public interface Person{
    public String getName();
    public void setName(String name);
}

For some reason this doesn't seem to work, as soon as I do that I start getting Errors when the JsonEncoderDecoder is generated. Has someone been able to achieve this?

Thanks!

도움이 되었습니까?

해결책

Why not define your interface and make your abstract class implement it?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top