gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:41:11 25 4
gpt4 key购买 nike

我有这样的服务:

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

这种情况我该怎么办?

最佳答案

您必须显式指定集合中实例的类型。在每个 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!!!"
}
]
}

关于java - 如何创建一个获取 POJO 集合的服务方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41647951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com