0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

WebClientのすべての例外を無視してそのまま返す

Posted at

WebClient素人です。

以前のRestTemplateでは以下ですべてのHttpStatusに対して例外をスローしなくなる。

    RestTemplate client = builder.errorHandler(new ResponseErrorHandler() {
      @Override
      public boolean hasError(ClientHttpResponse response) throws IOException {
        return false;
      }

      @Override
      public void handleError(ClientHttpResponse response) throws IOException {
      }
    }).build();

これと同じ事をWebClientをしたい……が、パっと見ではRestTemplateと同じやり方では出来ない、ように見えた。そこで https://gist.github.com/abhi2495/4607f2c827b2130ab6e08320f4ca5079 によると、@ExceptionHandlerを使うやり方が紹介されている。

	id 'org.springframework.boot' version '2.6.7'

以下は、status code, header, bodyをそのまま返している。

  @ExceptionHandler(value = WebClientResponseException.class)
  public ResponseEntity<String> handleWebClientException(WebClientResponseException ex) {
    return ResponseEntity
        .status(ex.getStatusCode())
        .headers(ex.getHeaders())
        .body(ex.getResponseBodyAsString());
  }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?