1

我有这样的服务:

public interface FireService {
    void addTags(String sessionId, List<TagCreateRequest> tags);
}

这里 TagCreateRequest 是:

@MetaClass(name = "...")
public class TagCreateRequest extends AbstractNotPersistentEntity implements Serializable {

    @MetaProperty(mandatory = true)
    protected TagType type;

    @MetaProperty(mandatory = true)
    protected Double time;

    @MetaProperty
    protected String text;

    public void setType(TagType type) {
        this.type = type;
    }

    public TagType getType() {
        return type;
    }

    public void setTime(Double time) {
        this.time = time;
    }

    public Double getTime() {
        return time;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

}

我的问题是当我尝试向这样的方法发出 REST 请求时addTags

http://localhost:8080/app/rest/v2/services/fire_FireService/addTags
{
    "sessionId": "1417270d-31cb-be3c-e583-4b172b4183a9",
    "tags": [
        {
            "type": "fire",
            "time": 12.333
        },
        {
            "type": "text",
            "time": 15.12,
            "text": "Test!!!"
        }
    ]
}

我得到的信息EntitySerializationException告诉我,MetaClass未定义实体:

EntitySerializationException: Cannot deserialize an entity. MetaClass is not defined

我试图查看平台如何确定MetaClass并发现奇怪的事情。如果服务参数是Collection,则传递MetaClass的是null

@Component("cuba_RestParseUtils")
public class RestParseUtils {
...
    public Object toObject(Class clazz, String value) throws ParseException {
    ...
        if (Collection.class.isAssignableFrom(clazz)) {
            return entitySerializationAPI.<Entity>entitiesCollectionFromJson(value, null);
        }
    ...
    }
...
}

在这种情况下我该怎么办?

4

1 回答 1

2

您必须明确指定集合中实例的类型。在每个TagCreateRequest实体中使用_entityName字段:

{
    "sessionId": "1417270d-31cb-be3c-e583-4b172b4183a9",
    "tags": [
        {
            "_entityName": "prj_TagMetaClassName",
            "type": "fire",
            "time": 12.333
        },
       {
            "_entityName": "prj_TagMetaClassName", 
            "type": "text",
            "time": 15.12,
            "text": "Test!!!"
       }
    ]
}
于 2017-01-16T05:51:15.647 回答