我需要获取在commercetools项目中定义的所有ProductType,因为我必须使用“名称”的本地化值在文件系统中执行搜索。基本上,我需要使用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);
}
}
我希望这回答了你的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句