提问者:小点点

如何使用API代码生成器与WebClient进行同步调用


RestTemplate留档中的Spring有注释:

从5.0开始,org. springframe.web.client.RestTemplate类处于维护模式,只有较小的更改请求和错误将被接受。请考虑使用org.springframe.web.reactive.client.WebClient,它具有更现代的API并支持同步、异步和流场景

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html

当我尝试使用Open Api Code生成器将RestTemplate替换为WebClient时,我无法进行同步调用。

pom. xml代码

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>${openapi-tool-version}</version>
    <executions>
        <execution>
            <id>Games</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <generatorName>java</generatorName>
                <library>webclient</library>
               <inputSpec>${project.basedir}/spec/games.yaml</inputSpec>
                <configOptions>
                    <configPackage>com.tintin.config</configPackage>
                    <apiPackage>com.tintin.api</apiPackage>
                    <modelPackage>com.tintin.model</modelPackage>
                    <invokerPackage>com.tintin.service</invokerPackage>
                    <sourceFolder>src/main/java</sourceFolder>
                    <dateLibrary>java8</dateLibrary>
                </configOptions>
                <generateModelTests>false</generateModelTests>
                <generateApiTests>false</generateApiTests>
            </configuration>
        </execution>
    </executions>
</plugin>

默认情况下,webclient正在进行异步调用并将响应包装在Mono中

public <T> Mono<T> invokeAPI(String path, HttpMethod method, Map<String, Object> pathParams, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
    final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, pathParams, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames);
    return requestBuilder.retrieve().bodyToMono(returnType);
}

预期输出(Simillar到rest模板)

public <T> ResponseEntity<T> invokeAPI(String path, HttpMethod method, Map<String, Object> pathParams, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
    final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, pathParams, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames);
    return requestBuilder.retrieve().bodyToMono(returnType).block();
}

如何在不对当前代码进行任何重大更改的情况下使用开放api代码生成器将Resttem替换为Webclient


共1个答案

匿名用户

您希望生成Rest Template类,但您使用的是