Skip to content

[REQ] Make a synchronous call with WebClient using Open API Code Generator #11678

@devanshu221

Description

@devanshu221

Spring in RestTemplate documentation has note:

As of 5.0 the class org.springframework.web.client.RestTemplate is in maintenance mode, with only minor requests for changes and bugs to be accepted going forward. Please, consider using the org.springframework.web.reactive.client.WebClient which has a more modern API and supports sync, async, and streaming scenarios

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

When I am trying to replace RestTemplate with WebClient using Open Api Code generator, I am unable to make Synchronous Calls.

pom.xml code

<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>

By default webclient is making Asynchronous call and wrapping the responses in 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);
}

Expected Output (Simillar to resttemplate)

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();
}

Can I replace Resttemplate with Webclient using open api code generator without making any major changes in my current code

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions