【问题标题】:GWT-Jackson-APT Couldn't make a guess for classnameGWT-Jackson-APT 无法猜测类名
【发布时间】:2019-02-13 03:55:48
【问题描述】:

在使用示例完成其余所有操作后,我绑定到序列化我的实际底层对象,并发现它总是会返回一个错误,即无法猜测我正在尝试序列化的类。这是我正在尝试做的高度简化的示例,以及对我来说似乎对如何做有意义的注释。

我想序列化原始(或盒装原始)对象的 List,在本例中为一个 int 和一个字符串。我的实际类也是所有原始(或盒装原始)类型。

@JSONMapper
public static interface TestMapper extends ObjectMapper<TestElmt>{
    TestMapper INSTANCE = new Webworkers_TestMapperImpl();
}

public static class TestElmt {

    List<test> inerVar = new ArrayList<>();

    public void addElement(test elmt){
        inerVar.add(elmt);
    }
    public List<test> getElements(){
        return inerVar;
    }

}

@JSONMapper
public static class test{

    public static test_MapperImpl MAPPER = new test_MapperImpl();

    int x;
    String y;

    test(int X,String Y){
        x = X;
        y = Y;
    }
}

但我得到的错误是:

Error:java: 创建源文件时出错 java.lang.IllegalArgumentException:无法猜测 client.myEnclosureClass.test

【问题讨论】:

  • 通常你不需要用JSONMapper标记测试类,注释接口或根类就足够了,说这可能是类名test的问题你试试Test isstead?

标签: java gwt gwt-jackson-apt


【解决方案1】:

问题中的代码有两个无法编译的问题:

首先测试类应该命名为Test - 大写T - 而不是test - small t -。

其次在类测试中应该有一个无参数的构造函数,否则反序列化器将不知道如何创建该类的新实例,它会生成但在其create方法中会出现编译错误。

如果我们像这样更改测试类,一切都应该工作

@JSONMapper
public static class Test {

    public static Test_MapperImpl MAPPER = new Test_MapperImpl();

    int x;
    String y;

    public Test() {
    }

    Test(int X, String Y){
            x = X;
            y = Y;
    }
}

这是因为 gwt-jackson-apt 做了一些假设并使用一些约定来生成底层的序列化器/反序列化器。

【讨论】:

    猜你喜欢
    • 2019-07-02
    • 2013-12-23
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 2018-08-24
    • 1970-01-01
    • 2020-08-24
    相关资源
    最近更新 更多