Skip to content

Enable error handling in Java WebClient library, fixes #1243 #1244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -470,22 +470,7 @@ public class ApiClient {
*/
public <T> Mono<T> invokeAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> 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 Mono.error(new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler"));
}
});
return requestBuilder.retrieve().bodyToMono(returnType);
}

/**
Expand All @@ -506,23 +491,7 @@ public class ApiClient {
*/
public <T> Flux<T> invokeFluxAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> 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 Flux.error(new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler"));
}
});
return requestBuilder.retrieve().bodyToFlux(returnType);
}

private WebClient.RequestBodySpec prepareRequest(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,22 +471,7 @@ public MediaType selectHeaderContentType(String[] contentTypes) {
*/
public <T> Mono<T> invokeAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> 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 Mono.error(new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler"));
}
});
return requestBuilder.retrieve().bodyToMono(returnType);
}

/**
Expand All @@ -507,23 +492,7 @@ public <T> Mono<T> invokeAPI(String path, HttpMethod method, MultiValueMap<Strin
*/
public <T> Flux<T> invokeFluxAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> 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 Flux.error(new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler"));
}
});
return requestBuilder.retrieve().bodyToFlux(returnType);
}

private WebClient.RequestBodySpec prepareRequest(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames) {
Expand Down