云端点:不允许使用数组或实体类型集合

时间:2013-10-30 23:39:30

标签: java google-app-engine google-cloud-endpoints

为什么Google Cloud Endpoints存在此限制:

Arrays or collections of entity types are not allowed.

对于使用方法的API:

@ApiMethod(name = "getCollection", path = "getCollection",  httpMethod = HttpMethod.POST)
public ArrayList<MyObject> getCollection(List<MyObject> pMyObjects) {

最好的解决方法是什么?谢谢!

1 个答案:

答案 0 :(得分:7)

我认为它不受支持的原因是因为方法签名中的命名参数最终是URL查询参数,并且他们不希望使用长项目列表来污染它。此外,它们仅支持签名中的实体类型的单个对象,因为它自动成为“请求主体”。你可以阅读它here in the docs

至于解决它,你为“请求体”创建一个容器实体对象。这样做的好处是API Explorer将在GUI中扩展实体对象的各个部分,并帮助您正确地执行JSON。

这是一个添加名为“patchFieldOps”的Map以实现部分更新的示例。您可以根据需要将多个字段放入Entity对象中。我认为如果嵌入更多用户定义的类型,他们还需要使用@Entity注释。

@Entity
public class EndpointUpdateRequestBody {
    // Since Google Cloud Endpoints doesn't support HTTP PATCH, we are overloading
    // HTTP PUT to do something similar.
    private Map<String, String> patchFieldsOps;

    public EndpointUpdateRequestBody() {
        patchFieldsOps = new HashMap<String, String>();
    }

    public EndpointUpdateRequestBody(Map<String, String> patchFieldsOps) {
        this.patchFieldsOps = patchFieldsOps;
    }

    public Map<String, String> getPatchFieldsOps() {
        return patchFieldsOps;
    }

    public void setPatchFieldsOps(Map<String, String> patchFieldsOps) {
        this.patchFieldsOps = patchFieldsOps;
    }
}

...

@ApiMethod(
        name = "stuff.update",
        path = "stuff/{id}",
        httpMethod = ApiMethod.HttpMethod.PUT
)
public Group update(
        User apiUser,
        @Named("id") String id,
        @Nullable @Named("name") String name,
        @Nullable @Named("description") String description,
        EndpointUpdateRequestBody requestBody)
        throws OAuthRequestException, InternalServerErrorException, NotFoundException,
        BadRequestException, UnauthorizedException, ConflictException {