Java 处理WebClient中JSON响应中的嵌入错误
我通过WebClient调用REST端点,并期望HTTP状态为Java 处理WebClient中JSON响应中的嵌入错误,java,spring,spring-boot,spring-webclient,Java,Spring,Spring Boot,Spring Webclient,我通过WebClient调用REST端点,并期望HTTP状态为200、40x、50x。如果我返回一个200/OK,我希望返回的JSON格式如下: { "returnStatus" : "ERROR", "returnMessage": "Client error description outside of HTTP header" } 我的网络客户端呼叫看起来像: getWebClient()
200、40x、50x
。如果我返回一个200/OK
,我希望返回的JSON格式如下:
{
"returnStatus" : "ERROR",
"returnMessage": "Client error description outside of HTTP header"
}
我的网络客户端呼叫看起来像:
getWebClient()
.get()
.uri(uriBuilder -> queryParameterMapper.mapQueryParams(uriBuilder, updateObj))
.retrieve()
.onStatus(
HttpStatus::is5xxServerError,
clientResponse -> handleServerError(clientResponse, updateObj))
.onStatus(
HttpStatus::is4xxClientError,
clientResponse -> handleClientError(clientResponse, updateObj))
.bodyToMono(JsonDto.class)
.retryWhen(createRetrySpec(tcUpdate))
.subscribe(updateObjDto -> handleSuccess(updateObjDto, updateObj));
因此,40x和50x错误得到处理。我现在想要实现的是我投入的
onStatus(HttpStatus::is2xxSuccessful, clientResponse -> handleSuccessEmbeddedError(clientResponse, updateObj))
进入网络客户端呼叫。
如果客户端在HTTP报头中返回200/OK
,但是响应JSON的“returnStatus”
包含“ERROR”
,我还想“retryWhen”
,否则(如果returnStatus=OK)一切正常,什么也不做(不要抛出任何异常,但“onStatus”
期望它)
你知道我是如何做到这一点的吗?WebClient也会处理嵌入的错误
先谢谢你