gpt4 book ai didi

java - 当 HTTP 请求返回状态 401 时,如何在 Java 中解析响应正文

转载 作者:IT老高 更新时间:2023-10-28 13:50:36 25 4
gpt4 key购买 nike

我正在使用 Spring 的 RestTemplate 和 Jackson 使用 RESTful JSON API。在某些情况下,我们可能会收到带有自定义 JSON 正文的 Status 401(未授权)响应,该响应由 API 制造商定义,如下所示:

{
"code": 123,
"message": "Reason for the error"
}

我们需要解析body,并在我们的业务逻辑中使用code属性。

这是我们需要解析的错误响应 Java 对象:

public class CustomError {

@JsonProperty
private Integer code;
@JsonProperty
private String message;

public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
}

还有一个自定义错误处理程序来执行此操作:

public class CustomErrorHandler extends DefaultResponseErrorHandler {
private RestTemplate restTemplate;
private ObjectMapper objectMapper;
private MappingJacksonHttpMessageConverter messageConverter;


@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
return super.hasError(response);
}

@Override
public void handleError(final ClientHttpResponse response) throws IOException {

try {
CustomError error =
(CustomError) messageConverter.read(CustomError.class, response);
throw new CustomErrorIOException(error, error.getMessage());
} catch (Exception e) {
// parsing failed, resort to default behavior
super.handleError(response);
}
}
}

错误处理程序失败,在 try block 中出现 HttpMessageNotReadableException:

"Could not read JSON: cannot retry due to server authentication, in streaming mode"

这就是我发送请求的方式:

restTemplate.postForObject(url, pojoInstance, responseClass);

如果使用普通的旧客户端程序(如 Postman)执行相同的请求,则会收到预期的 JSON 响应。因此,我认为问题可能出在 Spring 的 ClientHttpResponse 实现以某种方式不允许访问响应正文,以防 401 状态。

真的可以解析响应体吗?

更新

根据我的调查,RestTemplate 类使用 ClientHttpResponse 反过来创建 sun.net.www.protocol.http.HttpURLConnection提供输入流。它在那里,输入流被忽略并抛出 IOException:

cannot retry due to server authentication, in streaming mode

所以,HttpURLConnection 的实现导致了这个问题。

是否有可能避免这个问题?也许我们应该使用一种替代实现,在出现错误状态代码的情况下不忽略响应主体?你能推荐任何替代品吗?

最佳答案

在不需要自定义处理程序的情况下尝试以下方法。这个想法是从 HttpStatusCodeException 中获取作为字符串的响应,然后您可以将其转换为您的对象。对于转换,我使用了 Jackson 的 ObjectMapper:

        try {

restTemplate.postForObject(url, pojoInstance, responseClass);

} catch (HttpStatusCodeException e) {

if (e.getStatusCode() == HttpStatus.UNAUTHORIZED) {

String responseString = e.getResponseBodyAsString();

ObjectMapper mapper = new ObjectMapper();

CustomError result = mapper.readValue(responseString,
CustomError.class);
}
}

更新:使用不同的工厂也可能会有所帮助,因为默认工厂中存在与您的问题相关的错误(请参阅下面的评论):

RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());

关于java - 当 HTTP 请求返回状态 401 时,如何在 Java 中解析响应正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26676405/

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