"抽象类必须使用JsonTypeInfo"进行注释。编译DTO结构

时间:2014-12-22 18:58:24

标签: resty-gwt

我有一个非常简单的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 Polymorphic Encode/Decode issues when using an interface instead of an abstract class

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我最终得到了经典的试验和错误,忽略了restygwt的文档。以下是我为了使其发挥作用所做的事情:

  1. 删除了抽象类并将注释移动到界面。这种行为在official doc中并不十分清楚:&#34;其他类继承的超类必须是一个抽象类,并注释如下面的示例&#34;
  2. 当然改变了类来实现接口而不是扩展抽象类
  3. @Type(value = DataWithReplyDTO.class, name = "DataWithReplyDTO"),添加到界面@JsonSubTypes列表
  4. 我在单元测试中使用了以下编码器/解码器声明,它起作用了:

    public interface InterfaceDTOEncoderDecoder extends JsonEncoderDecoder<InterfaceDTO> {}
    

    结论:似乎你可以在一个接口下有几个级别的继承,只要你将接口中的所有类型声明为@JsonSubTypes

    ,它仍然可以工作
相关问题