Skip to content

[Bug]: 使用CompletableFuture.supplyAsync异步调用接口时出现java.lang.NullPointerException: Cannot invoke "com.dtflys.forest.mapping.MappingURLTemplate.isConstant()" because "urlTemplate" is null #248

@dawnFlyme

Description

@dawnFlyme

依赖环境:

Java: 1.8
Forest: 1.8
Backend: httpclient
SpringBoot: 2.7.18

问题简述:

使用CompletableFuture.supplyAsync,指定线程池时,调用接口时出现java.lang.NullPointerException: Cannot invoke "com.dtflys.forest.mapping.MappingURLTemplate.isConstant()" because "urlTemplate" is null

如何复现:

final List<CompletableFuture> classifyFutures = ocrReqDtos.stream().map(ocrReqDto -> CompletableFuture.supplyAsync(() -> {
// 构建ocr请求参数
LlmOcrFinalReqDto param = new LlmOcrFinalReqDto();
param.setFiles(Lists.newArrayList(ocrReqDto));
param.setTargetTypes(llmOcrTargetTypes);

        try {
            return Forest
                    .post("http://test-llmn:8090/api/v1/LLM/ocrcls")
                    .contentTypeJson()
                    .readTimeout(3000)
                    .addBody(JsonUtils.toJsonString(param))
                    .onSuccess((data, req, res) -> {
                    }).onError((ex, req, res) -> {
                    }).execute(LlmOcrRspDto.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    },taskExecutor())).collect(Collectors.toList());
    final CompletableFuture<Void> allOf = CompletableFuture.allOf(classifyFutures.toArray(new CompletableFuture[0]));
    allOf.get();
    List<LlmOcrRspDto> collect = classifyFutures.stream().map(CompletableFuture::join).collect(Collectors.toList());

预期结果:

正常调用

实际结果:

Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.lang.NullPointerException: Cannot invoke "com.dtflys.forest.mapping.MappingURLTemplate.isConstant()" because "urlTemplate" is null
at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:396)
at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2073)
at com.beetech.claimagent.Test.main(Test.java:64)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException: Cannot invoke "com.dtflys.forest.mapping.MappingURLTemplate.isConstant()" because "urlTemplate" is null
at com.beetech.claimagent.Test.lambda$main$2(Test.java:60)
at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Cannot invoke "com.dtflys.forest.mapping.MappingURLTemplate.isConstant()" because "urlTemplate" is null
at com.dtflys.forest.http.ForestURL.newURL(ForestURL.java:168)
at com.dtflys.forest.http.ForestURL.create(ForestURL.java:138)
at com.dtflys.forest.http.ForestRequest.setUrl(ForestRequest.java:824)
at com.dtflys.forest.http.ForestRequest.setUrl(ForestRequest.java:861)
at com.dtflys.forest.config.ForestConfiguration.post(ForestConfiguration.java:2386)
at com.dtflys.forest.config.ForestConfiguration.post(ForestConfiguration.java:2370)
at com.dtflys.forest.Forest.post(Forest.java:139)
at com.beetech.claimagent.Test.lambda$main$2(Test.java:52)

截图或视频:

Image

最小可复现代码:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions