Skip to content

Commit 95a11a7

Browse files
[Java][WebClient] add ResponseEntity return for webclient (#9327)
* feat: add ResponseEntity return for webclient fix: #5599 * refactor: merge functions simplification to avoir duplicated code
1 parent c7038d1 commit 95a11a7

File tree

10 files changed

+788
-89
lines changed

10 files changed

+788
-89
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
**/*.mustache linguist-vendored=true
2+
* text=auto eol=lf

modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache

+3-24
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import org.springframework.http.client.reactive.ClientHttpRequest;
3131
import org.springframework.web.client.RestClientException;
3232
import org.springframework.web.util.UriComponentsBuilder;
3333
import org.springframework.web.reactive.function.client.WebClient;
34+
import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
3435
import org.springframework.web.reactive.function.client.ClientResponse;
3536
import org.springframework.web.reactive.function.BodyInserter;
3637
import org.springframework.web.reactive.function.BodyInserters;
@@ -604,31 +605,9 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
604605
* @param returnType The return type into which to deserialize the response
605606
* @return The response body in chosen type
606607
*/
607-
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 {
608+
public <T> ResponseSpec 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 {
608609
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, pathParams, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames);
609-
return requestBuilder.retrieve().bodyToMono(returnType);
610-
}
611-
612-
/**
613-
* Invoke API by sending HTTP request with the given options.
614-
*
615-
* @param <T> the return type to use
616-
* @param path The sub-path of the HTTP URL
617-
* @param method The request method
618-
* @param pathParams The path parameters
619-
* @param queryParams The query parameters
620-
* @param body The request body object
621-
* @param headerParams The header parameters
622-
* @param formParams The form parameters
623-
* @param accept The request's Accept header
624-
* @param contentType The request's Content-Type header
625-
* @param authNames The authentications to apply
626-
* @param returnType The return type into which to deserialize the response
627-
* @return The response body in chosen type
628-
*/
629-
public <T> Flux<T> invokeFluxAPI(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 {
630-
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, pathParams, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames);
631-
return requestBuilder.retrieve().bodyToFlux(returnType);
610+
return requestBuilder.retrieve();
632611
}
633612

634613
private WebClient.RequestBodySpec prepareRequest(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) {

modules/openapi-generator/src/main/resources/Java/libraries/webclient/api.mustache

+26-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ import org.springframework.util.LinkedMultiValueMap;
1818
import org.springframework.util.MultiValueMap;
1919
import org.springframework.web.util.UriComponentsBuilder;
2020
import org.springframework.core.ParameterizedTypeReference;
21+
import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
2122
import org.springframework.web.reactive.function.client.WebClientResponseException;
2223
import org.springframework.core.io.FileSystemResource;
2324
import org.springframework.http.HttpHeaders;
2425
import org.springframework.http.HttpMethod;
2526
import org.springframework.http.HttpStatus;
2627
import org.springframework.http.MediaType;
28+
import org.springframework.http.ResponseEntity;
2729
import reactor.core.publisher.Mono;
2830
import reactor.core.publisher.Flux;
2931

@@ -62,7 +64,7 @@ public class {{classname}} {
6264
* @see <a href="{{url}}">{{summary}} Documentation</a>
6365
{{/externalDocs}}
6466
*/
65-
public {{#returnType}}{{#isArray}}Flux<{{{returnBaseType}}}>{{/isArray}}{{^isArray}}Mono<{{{returnType}}}>{{/isArray}} {{/returnType}}{{^returnType}}Mono<Void> {{/returnType}}{{operationId}}({{#allParams}}{{#isFile}}{{#useAbstractionForFiles}}{{#collectionFormat}}java.util.Collection<org.springframework.core.io.AbstractResource>{{/collectionFormat}}{{^collectionFormat}}org.springframework.core.io.AbstractResource{{/collectionFormat}}{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{dataType}}}{{/useAbstractionForFiles}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws WebClientResponseException {
67+
private ResponseSpec {{operationId}}RequestCreation({{#allParams}}{{#isFile}}{{#useAbstractionForFiles}}{{#collectionFormat}}java.util.Collection<org.springframework.core.io.AbstractResource>{{/collectionFormat}}{{^collectionFormat}}org.springframework.core.io.AbstractResource{{/collectionFormat}}{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{dataType}}}{{/useAbstractionForFiles}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws WebClientResponseException {
6668
Object postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
6769
{{#allParams}}
6870
{{#required}}
@@ -125,7 +127,29 @@ public class {{classname}} {
125127
String[] localVarAuthNames = new String[] { {{#authMethods}}"{{name}}"{{^-last}}, {{/-last}}{{/authMethods}} };
126128

127129
{{#returnType}}ParameterizedTypeReference<{{#isArray}}{{{returnBaseType}}}{{/isArray}}{{^isArray}}{{{returnType}}}{{/isArray}}> localVarReturnType = new ParameterizedTypeReference<{{#isArray}}{{{returnBaseType}}}{{/isArray}}{{^isArray}}{{{returnType}}}{{/isArray}}>() {};{{/returnType}}{{^returnType}}ParameterizedTypeReference<Void> localVarReturnType = new ParameterizedTypeReference<Void>() {};{{/returnType}}
128-
return apiClient.{{#isArray}}invokeFluxAPI{{/isArray}}{{^isArray}}invokeAPI{{/isArray}}("{{{path}}}", HttpMethod.{{httpMethod}}, pathParams, queryParams, postBody, headerParams, cookieParams, formParams, localVarAccept, localVarContentType, localVarAuthNames, localVarReturnType);
130+
return apiClient.invokeAPI("{{{path}}}", HttpMethod.{{httpMethod}}, pathParams, queryParams, postBody, headerParams, cookieParams, formParams, localVarAccept, localVarContentType, localVarAuthNames, localVarReturnType);
131+
}
132+
133+
/**
134+
* {{summary}}
135+
* {{notes}}
136+
{{#responses}} * <p><b>{{code}}</b>{{#message}} - {{message}}{{/message}}
137+
{{/responses}}{{#allParams}} * @param {{paramName}} {{description}}{{^description}}The {{paramName}} parameter{{/description}}
138+
{{/allParams}}{{#returnType}} * @return {{returnType}}
139+
{{/returnType}} * @throws WebClientResponseException if an error occurs while attempting to invoke the API
140+
{{#externalDocs}}
141+
* {{description}}
142+
* @see <a href="{{url}}">{{summary}} Documentation</a>
143+
{{/externalDocs}}
144+
*/
145+
public {{#returnType}}{{#isArray}}Flux<{{{returnBaseType}}}>{{/isArray}}{{^isArray}}Mono<{{{returnType}}}>{{/isArray}} {{/returnType}}{{^returnType}}Mono<Void> {{/returnType}}{{operationId}}({{#allParams}}{{#isFile}}{{#useAbstractionForFiles}}{{#collectionFormat}}java.util.Collection<org.springframework.core.io.AbstractResource>{{/collectionFormat}}{{^collectionFormat}}org.springframework.core.io.AbstractResource{{/collectionFormat}}{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{dataType}}}{{/useAbstractionForFiles}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws WebClientResponseException {
146+
{{#returnType}}ParameterizedTypeReference<{{#isArray}}{{{returnBaseType}}}{{/isArray}}{{^isArray}}{{{returnType}}}{{/isArray}}> localVarReturnType = new ParameterizedTypeReference<{{#isArray}}{{{returnBaseType}}}{{/isArray}}{{^isArray}}{{{returnType}}}{{/isArray}}>() {};{{/returnType}}{{^returnType}}ParameterizedTypeReference<Void> localVarReturnType = new ParameterizedTypeReference<Void>() {};{{/returnType}}
147+
return {{operationId}}RequestCreation({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}).{{#isArray}}bodyToFlux{{/isArray}}{{^isArray}}bodyToMono{{/isArray}}(localVarReturnType);
148+
}
149+
150+
public {{#returnType}}{{#isArray}}Mono<ResponseEntity<List<{{{returnBaseType}}}>>>{{/isArray}}{{^isArray}}Mono<ResponseEntity<{{{returnType}}}>>{{/isArray}} {{/returnType}}{{^returnType}}Mono<ResponseEntity<Void>> {{/returnType}}{{operationId}}WithHttpInfo({{#allParams}}{{#isFile}}{{#useAbstractionForFiles}}{{#collectionFormat}}java.util.Collection<org.springframework.core.io.AbstractResource>{{/collectionFormat}}{{^collectionFormat}}org.springframework.core.io.AbstractResource{{/collectionFormat}}{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{dataType}}}{{/useAbstractionForFiles}}{{/isFile}}{{^isFile}}{{{dataType}}}{{/isFile}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) throws WebClientResponseException {
151+
{{#returnType}}ParameterizedTypeReference<{{#isArray}}{{{returnBaseType}}}{{/isArray}}{{^isArray}}{{{returnType}}}{{/isArray}}> localVarReturnType = new ParameterizedTypeReference<{{#isArray}}{{{returnBaseType}}}{{/isArray}}{{^isArray}}{{{returnType}}}{{/isArray}}>() {};{{/returnType}}{{^returnType}}ParameterizedTypeReference<Void> localVarReturnType = new ParameterizedTypeReference<Void>() {};{{/returnType}}
152+
return {{operationId}}RequestCreation({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}}).{{#isArray}}toEntityList{{/isArray}}{{^isArray}}toEntity{{/isArray}}(localVarReturnType);
129153
}
130154
{{/operation}}
131155
}

samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java

+3-24
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.web.client.RestClientException;
3030
import org.springframework.web.util.UriComponentsBuilder;
3131
import org.springframework.web.reactive.function.client.WebClient;
32+
import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
3233
import org.springframework.web.reactive.function.client.ClientResponse;
3334
import org.springframework.web.reactive.function.BodyInserter;
3435
import org.springframework.web.reactive.function.BodyInserters;
@@ -589,31 +590,9 @@ public MediaType selectHeaderContentType(String[] contentTypes) {
589590
* @param returnType The return type into which to deserialize the response
590591
* @return The response body in chosen type
591592
*/
592-
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 {
593+
public <T> ResponseSpec 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 {
593594
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, pathParams, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames);
594-
return requestBuilder.retrieve().bodyToMono(returnType);
595-
}
596-
597-
/**
598-
* Invoke API by sending HTTP request with the given options.
599-
*
600-
* @param <T> the return type to use
601-
* @param path The sub-path of the HTTP URL
602-
* @param method The request method
603-
* @param pathParams The path parameters
604-
* @param queryParams The query parameters
605-
* @param body The request body object
606-
* @param headerParams The header parameters
607-
* @param formParams The form parameters
608-
* @param accept The request's Accept header
609-
* @param contentType The request's Content-Type header
610-
* @param authNames The authentications to apply
611-
* @param returnType The return type into which to deserialize the response
612-
* @return The response body in chosen type
613-
*/
614-
public <T> Flux<T> invokeFluxAPI(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 {
615-
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, pathParams, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames);
616-
return requestBuilder.retrieve().bodyToFlux(returnType);
595+
return requestBuilder.retrieve();
617596
}
618597

619598
private WebClient.RequestBodySpec prepareRequest(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) {

samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/api/AnotherFakeApi.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
import org.springframework.util.MultiValueMap;
1616
import org.springframework.web.util.UriComponentsBuilder;
1717
import org.springframework.core.ParameterizedTypeReference;
18+
import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
1819
import org.springframework.web.reactive.function.client.WebClientResponseException;
1920
import org.springframework.core.io.FileSystemResource;
2021
import org.springframework.http.HttpHeaders;
2122
import org.springframework.http.HttpMethod;
2223
import org.springframework.http.HttpStatus;
2324
import org.springframework.http.MediaType;
25+
import org.springframework.http.ResponseEntity;
2426
import reactor.core.publisher.Mono;
2527
import reactor.core.publisher.Flux;
2628

@@ -53,7 +55,7 @@ public void setApiClient(ApiClient apiClient) {
5355
* @return Client
5456
* @throws WebClientResponseException if an error occurs while attempting to invoke the API
5557
*/
56-
public Mono<Client> call123testSpecialTags(Client body) throws WebClientResponseException {
58+
private ResponseSpec call123testSpecialTagsRequestCreation(Client body) throws WebClientResponseException {
5759
Object postBody = body;
5860
// verify the required parameter 'body' is set
5961
if (body == null) {
@@ -81,4 +83,22 @@ public Mono<Client> call123testSpecialTags(Client body) throws WebClientResponse
8183
ParameterizedTypeReference<Client> localVarReturnType = new ParameterizedTypeReference<Client>() {};
8284
return apiClient.invokeAPI("/another-fake/dummy", HttpMethod.PATCH, pathParams, queryParams, postBody, headerParams, cookieParams, formParams, localVarAccept, localVarContentType, localVarAuthNames, localVarReturnType);
8385
}
86+
87+
/**
88+
* To test special tags
89+
* To test special tags and operation ID starting with number
90+
* <p><b>200</b> - successful operation
91+
* @param body client model
92+
* @return Client
93+
* @throws WebClientResponseException if an error occurs while attempting to invoke the API
94+
*/
95+
public Mono<Client> call123testSpecialTags(Client body) throws WebClientResponseException {
96+
ParameterizedTypeReference<Client> localVarReturnType = new ParameterizedTypeReference<Client>() {};
97+
return call123testSpecialTagsRequestCreation(body).bodyToMono(localVarReturnType);
98+
}
99+
100+
public Mono<ResponseEntity<Client>> call123testSpecialTagsWithHttpInfo(Client body) throws WebClientResponseException {
101+
ParameterizedTypeReference<Client> localVarReturnType = new ParameterizedTypeReference<Client>() {};
102+
return call123testSpecialTagsRequestCreation(body).toEntity(localVarReturnType);
103+
}
84104
}

0 commit comments

Comments
 (0)