如何创建一个获取POJO集合的服务方法

时间:2017-01-14 08:01:35

标签: java cuba-platform

我有这样的服务:

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;
    }

}

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

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,则传递MetaClassnull

@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);
        }
    ...
    }
...
}

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

1 个答案:

答案 0 :(得分:2)

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

lay_r_capture = (LinearLayout)findViewById(R.id.lay_r_capture);
LinearLayout.LayoutParams layparam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lay_r_capture.setOrientation(LinearLayout.HORIZONTAL);
lay_r_capture.setWeightSum(2f);
lay_r_capture.setLayoutParams(layparam);