English 中文(简体)
GWT 进行RPC时的序列化问题
原标题:GWT serialization issue while doing RPC

我在学习GWT。

我犯了一个错误 有关这圣洁。

我的问题的breif 解答

在类类中自定义

package com.exp.shared;


import java.io.Serializable;
import java.util.List;

public class Customproperties implements Serializable  {

    private Object value;
    private List<?> values;
          // more variable

    public Customproperties() {
        // TODO Auto-generated constructor stub
    }

    public Customproperties(String propertyName, List<?>  object,
            String propertyType, boolean mulitiValued, String cardinality, Boolean required) {

        this.propertyName=propertyName;
        this.values=object;
                // more initialization
    }

    public Customproperties(String propertyName,  List<?> object, String propertyType,
            boolean multiValued) {
        this.propertyName=propertyName;
        this.values=object;
                // more initialization
    }

    public Object getValue() {
              return value;
    }
    public List<?> getValues() {
        return values;
    }
 }

在类Impl 的服务器软件包中, I is 使用自定义对象

      if (doc.getPropertyValue("cmis:objectTypeId").toString().equals("cmis:document")) {

    customproperty=new Customproperties(p.getDefinition().getDisplayName(), p.getValue(), p.getType().toString(),p.isMultiValued());
       }
else {
    customproperty=new Customproperties(p.getDefinition().getDisplayName(), p.getValues(), p.getType().toString(),p.isMultiValued(),value.getCardinality(),value.isRequired());
            }

here in if condntion p.getValue() returns Object. and in else condition p.getValues() returns List.

CustomPropertyle 类中,当我将 object 变量 改为 string 时,它的工作非常顺利。

但我不改变它 它给了我错误。

com.exp.shared.Customproperties  was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.: instance = com.exp.shared.Customproperties@1aa5344
        at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:619)

我不想更改字符串 。 我只是要接收对象。 导致此对象可以是 string , date , int

Plzz帮助。

最佳回答

您应该阅读有关 RPC 序列化的 GWT Docs < a href="https:// developmenters.google.com/web-toolkit/doc/latest/DevGuideServer Communication#DevGuideSreatyTypes" rel="无跟踪"标题="这里"。

java. lang. 类。 对象不能序列, 因此不能指望一系列的物体类型会被串联到线上 。

这就是为什么你得到了例外。在您给定的代码中,您没有使用字段值。在您的类中,两个构建者只设置了数值列表。所以,如果您没有使用字段值,就删除它,它就会起作用。但假设这是一个错误,你确实需要使用它...

You will have to know all the different possible types your value can have. And then either you have different fields, like intValue, dateValue, stringValue ... Or you can have one String field, and serialize your objects into strings like this.

public class CustomProperties {
    private String value;
    private String type;

    private void setValue(Object value, String type) {
        if (value != null) {
            this.value = value.toString();
            this.type = type;
        时 时
    时 时

    private Object getValue() {
        if (value != null) {
            if ("int".equals(type)) return Integer.valueOf(value);
            elseif ("date".equals(type)) return // Parse date from value here
            elseif ("string".equals(type)) return (String) value;
            // other cases
        时 时
        return value;
    时 时

时 时

问题回答

In GWT when declaring containers never declare generic types like List.
Always use the more specific types like ArrayList.

在您的情况中,问题很可能是 List<? & gt; 。 GWT不知道您在列表中放置了何种类型,所以它没有生成代码来将源路径上每一种可能的类型序列化,而是除了它知道它已经需要的东西外,没有产生什么。当您试图将其他地方不需要的东西放在那里时,出现例外,这表明GWT没有被告知您打算将该物体发送到电线上。

这里的标准方法通常是创建一个标记界面, 可能执行 < code> serialable , 并使之成为 < code> List< myModelObjects> 。 那么所有适合的天体都应该安装该接口 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签