I use this code to send XML requests:
RestClient client = RestClientBuilder.builder()
.gatewayUrl(URL)
.build();
Mono<AuthorizeResponse> result = client.executeAndReceiveAuthorize(request);
response = result.block();
public RestClient(String gatewayUrl, String token, String username, String password, SslContext sslContext) {
this.token = token;
this.gatewayUrl = gatewayUrl;
WebClient.Builder builder = WebClient.builder().baseUrl(gatewayUrl);
if (sslContext != null) {
HttpClient httpClient = HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext));
ClientHttpConnector httpConnector = new ReactorClientHttpConnector(httpClient);
builder.clientConnector(httpConnector);
}
if (username != null && password != null) {
builder.filter(basicAuthentication(username, password));
}
client = builder.build();
}
public Mono<AuthorizeResponse> executeAndReceiveAuthorize(AuthorizeRequest transaction) {
Mono<AuthorizeRequest> transactionMono = Mono.just(transaction);
return client.post().uri(checkTrailingSlash(gatewayUrl) + token)
.header(HttpHeaders.USER_AGENT, "Mozilla/5.0")
.accept(MediaType.APPLICATION_XML)
.contentType(MediaType.APPLICATION_XML)
.body(transactionMono, AuthorizeRequest.class)
.retrieve()
.bodyToMono(AuthorizeResponse.class);
}
But sometimes I get this error:
org.springframework.web.reactive.function.client.WebClientResponseException$Unauthorized: 401 Unauthorized
at java.base/java.lang.Thread.run(Thread.java:834)
2019-08-27 22:05:45,720 ERROR [stderr] (processingTransactionGenesisAuthorizeContainer-1) Suppressed: java.lang.Exception: #block terminated with an error
Caused by: java.lang.NullPointerException: null
How I can catch and process the exception when I get error 401?
If it's possible I would like to process the exception after the line response = result.block();
.