0

我有一个非常简单的 DTO 结构,由一个接口、一个实现它的抽象类和抽象类下的类层次结构组成。界面:

public interface InterfaceDTO {}

抽象类:

import org.codehaus.jackson.annotate.JsonSubTypes;
import org.codehaus.jackson.annotate.JsonSubTypes.Type;
import org.codehaus.jackson.annotate.JsonTypeInfo;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "json_type")
@JsonSubTypes({
    @Type(value = DataDTO.class, name = "DataDTO"),
    @Type(value = VectorDTO.class, name = "VectorDTO") })
public abstract class AbstractDTO implements InterfaceDTO {}

一级班:

public class DataDTO extends AbstractDTO {
  private Short answerId;
  private String clientKey;
  private String answerText;
  .....

public class VectorDTO extends AbstractDTO {
  private Vector<InterfaceDTO> answers;

  public VectorDTO() {
    answers = new Vector<InterfaceDTO>();
  }

二级班:

public class DataWithReplyDTO extends DataDTO {
  private String replyData;

最后,restygwt JSON enc/dec 代码:

public interface InterfaceDTOCodec extends JsonEncoderDecoder<InterfaceDTO> {}
...
InterfaceDTOCodec codec = GWT.create(InterfaceDTOCodec.class);      
VectorDTO dto = new VectorDTO();
JSONValue json = codec.encode(dto);
InterfaceDTO other = codec.decode(json);
...

当我使用 GWT 2.6.1 和 restygwt 从 eclipse 编译时,我得到:

Compiling module com.test.web.MyApp
  Computing all possible rebind results for 'com.test.web.client.activity.InitActivity.InterfaceDTOCodec'
    Rebinding com.test.web.client.activity.InitActivity.InterfaceDTOCode
      Invoking generator org.fusesource.restygwt.rebind.JsonEncoderDecoderGenerator
        Generating: com.test.web.client.activity.InitActivity_InterfaceDTOCodec_Generated_ExtendedJsonEncoderDecoder_
          checking: org.fusesource.restygwt.client.JsonEncoderDecoder, type: class com.google.gwt.dev.javac.typemodel.JParameterizedType
          Generating: com.test.web.shared.dto.InterfaceDTO_Generated_JsonEncoderDecoder_
            [ERROR] Abstract classes must be annotated with JsonTypeInfo
  [ERROR] Errors in 'com/test/web/client/activity/InitActivity.java'
    [ERROR] Line 55: Failed to resolve 'com.test.web.client.activity.InitActivity.InterfaceDTOCodec' via deferred binding
  [WARN] For the following type(s), generated source was never committed (did you forget to call commit()?)
    [WARN] com.test.web.client.activity.InitActivity_InterfaceDTOCodec_Generated_ExtendedJsonEncoderDecoder_
    [WARN] com.test.web.shared.dto.InterfaceDTO_Generated_JsonEncoderDecoder_

按照指南,我向抽象类添加了注释,但仍然出现奇怪的错误消息。有什么帮助吗?

参考: http: //resty-gwt.github.io/documentation/restygwt-user-guide.html RestyGWT 使用接口而不是抽象类时的多态编码/解码问题

4

2 回答 2

1

尝试删除 DataWithReplyDTO 并查看它是否有效。

我猜你不能有 2 个级别的非抽象类。代码如何知道它是否需要创建 DataWithReplyDTO 或 DataDTO ?

于 2014-12-23T09:08:58.277 回答
0

我最终得到了经典的试验和错误,并忽略了来自 restygwt 的文档。这是我为使其工作所做的工作:

  1. 删除抽象类并将注解移至接口。这种行为在官方文档中不是很清楚:“其他类继承的超类必须是抽象类,并像下面的示例一样注释”
  2. 当然改变类来实现接口而不是扩展抽象类
  3. 添加@Type(value = DataWithReplyDTO.class, name = "DataWithReplyDTO"),到接口@JsonSubTypes列表

我在单元测试中使用了以下编码器/解码器声明并且它有效:

public interface InterfaceDTOEncoderDecoder extends JsonEncoderDecoder<InterfaceDTO> {}

底线:似乎您可以在一个接口下拥有多个继承级别,只要您将接口中的所有类型声明为@JsonSubTypes

于 2014-12-26T18:24:20.153 回答