春季REST模板垃圾错误响应主体

huangapple go评论73阅读模式
英文:

Spring rest template garbage error response body

问题

我有以下的Rest Template配置:

@Bean
public RestTemplate restTemplate() {
    RestTemplate template = new RestTemplate();
    template.getMessageConverters().add(new ObjectToUrlEncodedConverter());
    return template;
}

ObjectToUrlEncodedConverter 是直接从这个答案中复制过来的。

我按照下面的方式调用Rest API:

try {
    response = template.exchange(uri, httpRequestObject.getMethod(), requestEntity, httpRequestObject.getResponseClass());
    responseObject.setHeaders(response.getHeaders().toSingleValueMap());
    responseObject.setHttpStatus(response.getStatusCode());
    responseObject.setResponseBody(response.getBody());
} catch (HttpStatusCodeException ex) {
    log.error("服务器返回非OK响应。状态码:{},响应:{}", ex.getStatusCode(), ex.getResponseBodyAsString());
    throw ex;
}

我遇到的问题是,当响应是4xx或5xx系列时,响应体 ex.getResponseBodyAsString() 返回的是一些垃圾值。类似于这样:�0E%ܙ6�1�Y������hP��N��\]8���w���+Q��\�:S�Ȕ�������뚈M�&#39;Z���ml�)�@��]B͎�L؏4Ğ V�`�V�Qx�F<{Q���/fZ�������S�3RNy��>ć��]S���+Е=��!�x9��)Ԏ��n%������

我已经尝试将 Charset 设置为 "UTF-8",就像下面这样获取错误响应体一样:

ex.getResponseBodyAsString(Charset.forName("UTF-8"))

同时,还尝试从 responseBodyByteArray 构建字符串,如下所示:

new String(ex.getResponseBodyAsByteArray())

但仍然没有成功。

相同的端点在 Postman 中运行良好。Rest Template 似乎在某种情况下搞乱了错误响应体。

英文:

I have the following rest template config:

@Bean
public RestTemplate restTemplate() {
    RestTemplate template = new RestTemplate();
    template.getMessageConverters().add(new ObjectToUrlEncodedConverter());
    return template;
}

ObjectToUrlEncodedConverter is copied as is from this answer

And I call the rest API as mentioned below:

try {
        response = template.exchange(uri, httpRequestObject.getMethod(), requestEntity, httpRequestObject.getResponseClass());
        responseObject.setHeaders(response.getHeaders().toSingleValueMap());
        responseObject.setHttpStatus(response.getStatusCode());
        responseObject.setResponseBody(response.getBody());
    } catch (HttpStatusCodeException ex) {
        log.error(&quot;Non OK response received from server. Status code: {}, response: {}&quot;, ex.getStatusCode(), ex.getResponseBodyAsString());
        throw ex;
    }

The problem I am facing is, when the response is 4xx or 5xx series, The response body ex.getResponseBodyAsString() is giving garbage values. Something like this: �0E%ܙ6�1�Y������hP��N��\]8���w���+Q��\�:S�Ȕ�������뚈M�&#39;Z���ml�)�@��]B͎�L؏4Ğ V�`�V�Qx�F<{Q���/fZ�������S�3RNy��>ć��]S���+Е=��!�x9��)Ԏ��n%������

I have tried setting the Charset to "UTF-8" while getting the errorResponseBody like the following.

ex.getResponseBodyAsString(Charset.forName(&quot;UTF-8&quot;))

also, building the string from responseBodyByteArray as follows:

new String(ex.getResponseBodyAsByteArray())

But still no luck.

The same endpoint works well in Postman. Rest template seems to be messing up the error response body somehow.

答案1

得分: 0

我不太确定问题究竟是什么。我将配置余下的模板以使用 Apache Http 客户端,而不是默认的 HttpUrlConnection 用于余下的模板。

这似乎很有效,并修复了错误响应体的问题。

这个教程 帮助我们实现了相同的操作。

英文:

I am not sure what the problem was exactly. I configured the rest template to use apache Http client instead of the default HttpUrlConnection used by rest template.

This seemed to work well and fixed the garbage error response body issue.

This tutorial helps us do the same.

huangapple
  • 本文由 发表于 2020年9月14日 23:15:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63887160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定