解析WebClient对自定义错误对象的响应?

问题描述 投票:0回答:1

我有此代码,我使用cxf WebClient:

WebClient client = someClient.reset();
Response response = client.post(bodyRequest);

如果响应中的状态码变为200,我可以将其解析为类似以下内容:

CustomResponse customResponse = response.readEntity(CustomResponse.class);

并且没关系,但是如果状态码变为400或另一个,响应实体似乎为空,因此我找不到找到将其解析为对象ResponseCodeError的方法,如下所示:

ResponseCodeError responseError= response.readEntity(ResponseCodeError.class);

这将失败。

是否有使用cxf并将错误解析为Custom错误类的方法?

谢谢。

java cxf
1个回答
0
投票

您可以使用]查看状态>

int code = response.getStatus();

然后您检查代码200以解析实体,或为其他代码(例如400)抛出相应的错误。

© www.soinside.com 2019 - 2024. All rights reserved.