当使用接口而不是抽象类时RestyGWT Polymorphic编码/解码问题 [英] RestyGWT Polymorphic Encode/Decode issues when using an interface instead of an abstract class

查看:112
本文介绍了当使用接口而不是抽象类时RestyGWT Polymorphic编码/解码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据RestyGWT 文档,必须使用抽象超级例如,给定:

  @JsonSubTypes(@Type(value = PersonImpl.class,name = PersonImpl))
@JsonTypeInfo(use = Id.NAME,include = As.PROPERTY,property =@ class)
public abstract String 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;






$ b如果我使用定义的编码器/解码器, :

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

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

我试图做一些非常相似但差别很小的事情,也就是 Person 是一个抽象类,我希望它成为一个接口:

  @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);
}

出于某种原因,这似乎不起作用,只要我做当生成JsonEncoderDecoder时,我开始出现错误。有人能够做到这一点吗?



谢谢!

解决方案

为什么不定义你的接口并让你的抽象类实现呢?

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?

这篇关于当使用接口而不是抽象类时RestyGWT Polymorphic编码/解码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆