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

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

  •  29-06-2022
  •  | 
  •  

Question

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!

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top