From b6a937883969da0345c34d4c4aa0301fffcfb647 Mon Sep 17 00:00:00 2001 From: Lukas Steinbrecher Date: Mon, 15 Oct 2018 12:01:16 +0200 Subject: [PATCH 1/2] enable error handling in Java WebClient library, fixes #1243 --- .../libraries/webclient/ApiClient.mustache | 21 +++++++++++++++++-- .../org/openapitools/client/ApiClient.java | 21 +++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache index 7f91fe441341..9c3000c4adff 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache @@ -452,6 +452,23 @@ public class ApiClient { return isForm ? BodyInserters.fromMultipartData(formParams) : (obj != null ? BodyInserters.fromObject(obj) : null); } + /** + * Handles an unsuccessful server response. + * This method is called when the server returns a HTTP status code != 2xx. + * + * The default behaviour is to wrap the unsuccessful response in an {@code ApiException} + * and terminate the reactive stream with the exception. + * + * Can be overwritten to implement custom error handling. + * + * @param response the unsuccessful HTTP response + * @param + * @return reactive stream in error state + */ + protected Mono handleUnsuccessfulResponse(ClientResponse response) { + return Mono.error(new ApiException(response.statusCode().toString(), null, response.statusCode().value(), response.headers().asHttpHeaders())); + } + /** * Invoke API by sending HTTP request with the given options. * @@ -483,7 +500,7 @@ public class ApiClient { return response.bodyToMono(returnType); } } else { - return Mono.error(new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler")); + return handleUnsuccessfulResponse(response); } }); } @@ -520,7 +537,7 @@ public class ApiClient { return response.bodyToFlux(returnType); } } else { - return Flux.error(new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler")); + return handleUnsuccessfulResponse(response); } }); } diff --git a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java index 7d5f7dd66bb4..37b7e063afc6 100644 --- a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java @@ -453,6 +453,23 @@ public MediaType selectHeaderContentType(String[] contentTypes) { return isForm ? BodyInserters.fromMultipartData(formParams) : (obj != null ? BodyInserters.fromObject(obj) : null); } + /** + * Handles an unsuccessful server response. + * This method is called when the server returns a HTTP status code != 2xx. + * + * The default behaviour is to wrap the unsuccessful response in an {@code ApiException} + * and terminate the reactive stream with the exception. + * + * Can be overwritten to implement custom error handling. + * + * @param response the unsuccessful HTTP response + * @param + * @return reactive stream in error state + */ + protected Mono handleUnsuccessfulResponse(ClientResponse response) { + return Mono.error(new ApiException(response.statusCode().toString(), null, response.statusCode().value(), response.headers().asHttpHeaders())); + } + /** * Invoke API by sending HTTP request with the given options. * @@ -484,7 +501,7 @@ public Mono invokeAPI(String path, HttpMethod method, MultiValueMap Flux invokeFluxAPI(String path, HttpMethod method, MultiValueMap Date: Sun, 9 Dec 2018 13:37:16 +0100 Subject: [PATCH 2/2] remove custom error handling logic in Java WebClient library, fixes #1243 --- .../libraries/webclient/ApiClient.mustache | 52 +------------------ .../org/openapitools/client/ApiClient.java | 52 +------------------ 2 files changed, 4 insertions(+), 100 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache index 9c3000c4adff..1c3a2a6a77e6 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache @@ -452,23 +452,6 @@ public class ApiClient { return isForm ? BodyInserters.fromMultipartData(formParams) : (obj != null ? BodyInserters.fromObject(obj) : null); } - /** - * Handles an unsuccessful server response. - * This method is called when the server returns a HTTP status code != 2xx. - * - * The default behaviour is to wrap the unsuccessful response in an {@code ApiException} - * and terminate the reactive stream with the exception. - * - * Can be overwritten to implement custom error handling. - * - * @param response the unsuccessful HTTP response - * @param - * @return reactive stream in error state - */ - protected Mono handleUnsuccessfulResponse(ClientResponse response) { - return Mono.error(new ApiException(response.statusCode().toString(), null, response.statusCode().value(), response.headers().asHttpHeaders())); - } - /** * Invoke API by sending HTTP request with the given options. * @@ -487,22 +470,7 @@ public class ApiClient { */ public Mono invokeAPI(String path, HttpMethod method, MultiValueMap queryParams, Object body, HttpHeaders headerParams, MultiValueMap formParams, List accept, MediaType contentType, String[] authNames, ParameterizedTypeReference returnType) throws RestClientException { final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, queryParams, body, headerParams, formParams, accept, contentType, authNames); - - return requestBuilder.exchange() - .flatMap(response -> { - HttpStatus statusCode = response.statusCode(); - if (response.statusCode() == HttpStatus.NO_CONTENT) { - return Mono.empty(); - } else if (statusCode.is2xxSuccessful()) { - if (returnType == null) { - return Mono.empty(); - } else { - return response.bodyToMono(returnType); - } - } else { - return handleUnsuccessfulResponse(response); - } - }); + return requestBuilder.retrieve().bodyToMono(returnType); } /** @@ -523,23 +491,7 @@ public class ApiClient { */ public Flux invokeFluxAPI(String path, HttpMethod method, MultiValueMap queryParams, Object body, HttpHeaders headerParams, MultiValueMap formParams, List accept, MediaType contentType, String[] authNames, ParameterizedTypeReference returnType) throws RestClientException { final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, queryParams, body, headerParams, formParams, accept, contentType, authNames); - - return requestBuilder.exchange() - .flatMapMany(response -> { - HttpStatus statusCode = response.statusCode(); - ClientResponse.Headers headers = response.headers(); - if (response.statusCode() == HttpStatus.NO_CONTENT) { - return Flux.empty(); - } else if (statusCode.is2xxSuccessful()) { - if (returnType == null) { - return Flux.empty(); - } else { - return response.bodyToFlux(returnType); - } - } else { - return handleUnsuccessfulResponse(response); - } - }); + return requestBuilder.retrieve().bodyToFlux(returnType); } private WebClient.RequestBodySpec prepareRequest(String path, HttpMethod method, MultiValueMap queryParams, Object body, HttpHeaders headerParams, MultiValueMap formParams, List accept, MediaType contentType, String[] authNames) { diff --git a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java index 37b7e063afc6..2739ada0a734 100644 --- a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java @@ -453,23 +453,6 @@ public MediaType selectHeaderContentType(String[] contentTypes) { return isForm ? BodyInserters.fromMultipartData(formParams) : (obj != null ? BodyInserters.fromObject(obj) : null); } - /** - * Handles an unsuccessful server response. - * This method is called when the server returns a HTTP status code != 2xx. - * - * The default behaviour is to wrap the unsuccessful response in an {@code ApiException} - * and terminate the reactive stream with the exception. - * - * Can be overwritten to implement custom error handling. - * - * @param response the unsuccessful HTTP response - * @param - * @return reactive stream in error state - */ - protected Mono handleUnsuccessfulResponse(ClientResponse response) { - return Mono.error(new ApiException(response.statusCode().toString(), null, response.statusCode().value(), response.headers().asHttpHeaders())); - } - /** * Invoke API by sending HTTP request with the given options. * @@ -488,22 +471,7 @@ protected Mono handleUnsuccessfulResponse(ClientResponse response) { */ public Mono invokeAPI(String path, HttpMethod method, MultiValueMap queryParams, Object body, HttpHeaders headerParams, MultiValueMap formParams, List accept, MediaType contentType, String[] authNames, ParameterizedTypeReference returnType) throws RestClientException { final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, queryParams, body, headerParams, formParams, accept, contentType, authNames); - - return requestBuilder.exchange() - .flatMap(response -> { - HttpStatus statusCode = response.statusCode(); - if (response.statusCode() == HttpStatus.NO_CONTENT) { - return Mono.empty(); - } else if (statusCode.is2xxSuccessful()) { - if (returnType == null) { - return Mono.empty(); - } else { - return response.bodyToMono(returnType); - } - } else { - return handleUnsuccessfulResponse(response); - } - }); + return requestBuilder.retrieve().bodyToMono(returnType); } /** @@ -524,23 +492,7 @@ public Mono invokeAPI(String path, HttpMethod method, MultiValueMap Flux invokeFluxAPI(String path, HttpMethod method, MultiValueMap queryParams, Object body, HttpHeaders headerParams, MultiValueMap formParams, List accept, MediaType contentType, String[] authNames, ParameterizedTypeReference returnType) throws RestClientException { final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, queryParams, body, headerParams, formParams, accept, contentType, authNames); - - return requestBuilder.exchange() - .flatMapMany(response -> { - HttpStatus statusCode = response.statusCode(); - ClientResponse.Headers headers = response.headers(); - if (response.statusCode() == HttpStatus.NO_CONTENT) { - return Flux.empty(); - } else if (statusCode.is2xxSuccessful()) { - if (returnType == null) { - return Flux.empty(); - } else { - return response.bodyToFlux(returnType); - } - } else { - return handleUnsuccessfulResponse(response); - } - }); + return requestBuilder.retrieve().bodyToFlux(returnType); } private WebClient.RequestBodySpec prepareRequest(String path, HttpMethod method, MultiValueMap queryParams, Object body, HttpHeaders headerParams, MultiValueMap formParams, List accept, MediaType contentType, String[] authNames) {