gpt4 book ai didi

spring - 如何在 Webflux WebClient 中自定义异常?

转载 作者:行者123 更新时间:2023-12-03 23:10:55 33 4
gpt4 key购买 nike

在我的 webflux 应用程序中,我想通过 WebClient 发送一些请求。我想处理所有条件( 200 401 403 和 和 和 和 响应,然后响应客户 19 和 json 响应 ...9对于错误状态代码,我想使用@RestControllerAdvice,所以我必须抛出一个自定义异常,然后在 Controller 通知中处理自定义 json。查看示例代码:

WebClient.create().post
.uri("URI")
.retrieve()
.onStatus(HttpStatus::is4xxClientError, {
// create Mono<CustomException>
}
.bodyToMono(ResponseDto.class)

现在异常处理程序如下:
@ResponseBody
@ResponseStatus(...)
@ExceptionHandler(CustomException1.class)
public void customException1(CustomException1 exception) {
//do Something width response body
}

@ExceptionHandler(CustomException2.class)
public void customException2(CustomException2 exception) {
//do Something width response body
}

Webclient 得到一个 401 响应,带有 json 正文,如下所示:
{
"message": "Password is incorrect"
}

我可以创建 Mono.error(new CustomException()) ,但问题是 WebClient 响应正文。如果消息是“密码不正确”,我想发送客户端:

Username or Password is incorrect



我该怎么办?

最佳答案

请检查以下 API 实现,它处理调用的 rest api 调用的错误响应。我假设我们知道 api 的响应,所以我创建了 ResponseDTO类(class)。
Controller .java

@GetMapping("/webclient/test")
public ResponseDTO testWebclient(@RequestBody RequestDTO requestPayload) {
String url = "http://localhost:8080/api/write/simple";
return WebClient.create()
.post()
.uri(url)
.accept(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromObject(requestPayload))
.retrieve()
.onStatus(HttpStatus::is4xxClientError, response -> {
return response.bodyToMono(ResponseDTO.class).flatMap(error -> {
return Mono.error(new CustomRuntimeException(error.getMessage(), response.statusCode(), error.getErrorCode()));
});
})
.onStatus(HttpStatus::is5xxServerError, response -> {
return response.bodyToMono(ResponseDTO.class).flatMap(error -> {
return Mono.error(new CustomRuntimeException(error.getMessage(), response.statusCode()));
});
})
.bodyToMono(ResponseDTO.class);
}
响应DTO.java
public class ResponseDTO {

private String message;
private HttpStatus status;
private ErrorCode errorCode;

public ResponseDTO(String message, HttpStatus status) {
this.message = message;
this.status = status;
}

public ResponseDTO(String message, HttpStatus status, ErrorCode errorCode) {
this.message = message;
this.status = status;
this.errorCode = errorCode;
}
/** getters **/
}
错误代码.java
    public class ErrorCode {
private String numCode;
private String txtCode;

/** getters **/
}
要捕获异常,请在 @RestControllerAdvice 中添加以下代码片段(希望您已经知道)的课。
@ExceptionHandler(value = CustomRuntimeException.class)
public final ResponseEntity<ApiResponse> handleCustomRuntimeException(CustomRuntimeException exception) {
List<ApiSubError> subErrors = Arrays.asList(new ApiSubError(exception.getMessage()));
ApiResponse response = new ApiErrorResponse(exception.getMessage(), exception.getErrorCode());
LOG.error("result: {}", response.getResult());
return new ResponseEntity<>(response, exception.getHttpStatus());
}

关于spring - 如何在 Webflux WebClient 中自定义异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57652631/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com