【问题标题】:GWT Serialization - class has no instantiable subtypesGWT 序列化 - 类没有可实例化的子类型
【发布时间】:2016-08-30 13:23:15
【问题描述】:

我正在尝试序列化这个对象 (OperatorDTO),以便我可以使用 RPC 将它从我的服务器端发送到客户端。我已经阅读了有关此主题的其他帖子,但我不明白为什么我做的事情与其他人不同。

当我运行我的项目时,会发生此错误

Compiling module edu.example.RPCExample
         Computing all possible rebind results for 'edu.example.client.service.ExampleService'
            Rebinding edu.example.client.service.ExampleService
               Invoking generator com.google.gwt.user.rebind.rpc.ServiceInterfaceProxyGenerator
                  Generating client proxy for remote service interface 'edu.example.client.service.ExampleService'
                     [ERROR] 'edu.example.client.models.OperatorDTO' has no instantiable subtypes
         [ERROR] Errors in 'edu/example/client/service/ExampleServiceClientImpl.java'
            [ERROR] Line 18: Failed to resolve 'edu.example.client.service.ExampleService' via deferred binding
         Unification traversed 16362 fields and methods and 1526 types. 1494 are considered part of the current module and 1494 had all of their fields and methods traversed.
      [ERROR] Compiler returned false
      [WARN] recompile failed
      [WARN] continuing to serve previous version

当我实现 io.serializable 并且只在类中使用基本数据类型时,我不明白为什么会收到错误“OperatorDTO 没有可实例化的子类型”。我在类中使用的东西在序列化时是不允许的吗?

package edu.example.client.models;
import java.io.Serializable;

public final class OperatorDTO implements Serializable
{   
    private static final long serialVersionUID = 1L;
    private final int ID_MINIMUM_VALUE = 11;
    private final int ID_MAXIMUM_VALUE = 99;
    private final int RANK_MINIMUM_VALUE = -1;
    private final int RANK_MAXIMUM_VALUE = 1;
    private final int NAME_MINIMUM_LENGTH = 2;
    private final int PASSWORD_MINIMUM_LENGTH = 6;
    private final int NUMBER_OF_SPECIAL_CHARACTERS = 3;

    private int oprID;
    private String oprNavn;
    private String ini;
    private String cpr;
    private String password;
    private int rank;

public OperatorDTO(int oprID, String oprNavn, String ini, String cpr, String password, int rank) {
        setOprID(oprID);
        setName(oprNavn);
        setIni(ini);
        setCpr(cpr);
        setPassword(password);
        setRank(rank);
    }

    public OperatorDTO(int oprID, String oprNavn, String ini, String cpr, String password) {
        setOprID(oprID);
        setName(oprNavn);
        setIni(ini);
        setCpr(cpr);
        setPassword(password);
    }

    public void setIni(String ini) {
        this.ini = ini;
    }

    public String getIni() {
        return this.ini;
    }

    public void setRank(int rank) {
        if(rank >= RANK_MINIMUM_VALUE && rank <= RANK_MAXIMUM_VALUE)
            this.rank = rank;kravende");
    }

    public int getRank() {
        return this.rank;
    }

    public void setOprID(int oprID) {
        if(oprID >= ID_MINIMUM_VALUE && oprID <= ID_MAXIMUM_VALUE)
            this.oprID = oprID;
    }

    public int getOprID() {
        return this.oprID;
    }

    public void setName(String name) {
        if(name != null)
            if(name.length() >= NAME_MINIMUM_LENGTH)
                this.oprNavn = name;
    }

    public void setCpr(String cpr) {
        this.cpr = cpr;
    }

    public void setPassword(String password) {
        if(valPass(password))
            this.password = password;
    }

    public String getName() {
        return this.oprNavn;
    }

    public String getCpr() {
        return this.cpr;
    }

    public String getPassword() {
        return this.password;
    }

    private boolean valPass(String pass) {
        if(pass.length() < PASSWORD_MINIMUM_LENGTH)
            return false;

        byte lowerCase = 0;
        byte upperCase = 0;
        byte digit = 0;
        byte specialChar = 0;
        char[] passChar = pass.toCharArray();

        for (char c : passChar) {
            if(Character.isLowerCase(c)) 
                lowerCase = 1;
            else if(Character.isUpperCase(c))
                upperCase = 1;
            else if(Character.isDigit(c)) 
                digit = 1;
            else if(c == 46 || c == 45 || c == 95 || c == 43 || c ==33 || c == 63 || c == 61) 
                specialChar = 1;
            else 
                return false;
        }
        return (lowerCase + upperCase + digit + specialChar) >= NUMBER_OF_SPECIAL_CHARACTERS;
    }

    @Override
    public String toString() {
        return "OperatorDTO{" + "oprID=" + oprID + ", oprNavn=" + oprNavn + ", ini=" + ini + ", cpr=" + cpr + ", password=" + password + ", rank=" + rank + '}';
    }
}

提前感谢您的帮助!

【问题讨论】:

    标签: java serialization gwt rpc


    【解决方案1】:

    GWT 需要一个默认构造函数。

    http://www.gwtproject.org/doc/latest/tutorial/RPC.html#serialize

    【讨论】:

    • 哇,这很简单.. xD 非常感谢!
    • 它还需要实现Serializable - 只花了半个小时才意识到我忽略了那个小细节......
    • 你可以使用SerializableIsSerializable
    • 这就像您可能会收到此错误的 100 个随机原因中的 1 个。
    • @TomášZato 错误日志和相应的代码在这种情况下很清楚
    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 2019-09-26
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 2015-05-29
    • 2011-12-01
    • 1970-01-01
    相关资源
    最近更新 更多