如何获取在我的 commercetools 项目中创建的所有 ProductType?

How to obtain all ProductTypes created in my commercetools project?

我需要获取在我的 commercetools 项目中定义的所有 ProductType,因为我必须使用 "name" 的本地化值在文件系统中执行搜索。 基本上我需要使用 JVM SDK 来提取 ProductTypes 列表并遍历它。

谁能告诉我如何实现它?

提前致谢。

是的,使用 jvm sdk 非常可行,这里是一个代码片段,说明如何做到这一点

package io.sphere.sdk.deletemeplese;

import io.sphere.sdk.producttypes.ProductType;
import io.sphere.sdk.producttypes.queries.ProductTypeQuery;
import io.sphere.sdk.queries.PagedQueryResult;
import io.sphere.sdk.test.IntegrationTest;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.assertj.core.api.Assertions.assertThat;

public class SomeIntegrationTest extends IntegrationTest {


    @Test
    public void test(){

        final int PAGE_SIZE = 500;
        final long totalProductTypes = client().executeBlocking(ProductTypeQuery.of().withLimit(0)).getTotal();

        List<ProductType> allProductTypes = IntStream.range(0,(int)(totalProductTypes/PAGE_SIZE) +1)
                .mapToObj(i->i)
                .map(i -> ProductTypeQuery.of().withLimit(500).withOffset(i*PAGE_SIZE))
                .map(client()::executeBlocking)
                .map(PagedQueryResult::getResults)
                .flatMap(List::stream)
                .collect(Collectors.toList());

        assertThat(allProductTypes).hasSize((int)totalProductTypes);

    }

}

我希望这能回答你的问题。

您可以使用 class QueryExecutionUtils.

CompletionStage<List<ProductType>> allProductTypesStage = 
       QueryExecutionUtils.queryAll(client, ProductTypeQuery.of());