2

I am writing a rest proxy (it exposes the API and delegates call to other server) and it works fine for the normal case and also for 500 http status code, we get the response from the rest client. But when we get 404 status code, the Rest API server returns the message but we get junk values from the RestTemplate. We need to pass the same response to other API user but cannot get the same response.

Message returned from REST API Server:


{ "status_code":"0", "error":{
"code":"404", "description":"Source not found" } }

Getting the below response by RestTemplate client:

Not able to paste the content, attaching the screen shot of the response. Response for Http 404 error Please see the code below.

@RequestMapping(value = "/api/**")
public @ResponseBody String apiProxy(@RequestBody String body, HttpMethod method, HttpServletRequest request,
        HttpServletResponse response) throws URISyntaxException {
    RestTemplate restTemplate = new RestTemplate(
            new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
    restTemplate.setInterceptors(Collections.singletonList(new RestClientLoggingInterceptor()));
    restTemplate.setErrorHandler(new CustomResponseErrorHandler());
    restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

    HttpHeaders httpHeaders = new HttpHeaders();
    Enumeration<String> headers = request.getHeaderNames();
    String headerName = null;
    String headerValue = null;
    while (headers.hasMoreElements()) {
        headerName = headers.nextElement();
        headerValue = request.getHeader(headerName);
        httpHeaders.set(headerName, headerValue);
    }
    HttpEntity<String> httpEntity = new HttpEntity<String>(body, httpHeaders);

    URI uri = new URI(ServerProtocol, null, ServerDomain, Integer.valueOf(ServerPort),
            request.getRequestURI(), request.getQueryString(), null);

    ResponseEntity<String> responseEntity = null;
    try {
        responseEntity = restTemplate.exchange(uri, method, httpEntity, String.class);
    } catch (RestClientResponseException e) {
        response.setStatus(e.getRawStatusCode());
        return e.getResponseBodyAsString();
    }
    response.setStatus(responseEntity.getStatusCode().value());
    return responseEntity.getBody();
}

ResponseErrorHandler Class

   public class CustomResponseErrorHandler extends DefaultResponseErrorHandler {

        private static final Logger logger = LogManager.getLogger(CustomResponseErrorHandler.class);

        @Override
        public void handleError(ClientHttpResponse response) throws IOException {
            logger.error("Response error: {} {}", response.getStatusCode(), response.getStatusText());
        }

    }

RestClientLoggingInterceptor Class

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
                    throws IOException {
                ClientHttpResponse response = execution.execute(request, body);
                logger.debug("request method:" + request.getMethod());
                logger.debug("request URI:" + request.getURI());
                logger.debug("request headers:" + request.getHeaders());
                logger.debug("request body:" + new String(body, Charset.forName("UTF-8")));
                logger.debug("response status code:" + response.getStatusCode());
                logger.debug("response headers:" + response.getHeaders());
                logger.debug("response body:" + IOUtils.toString(response.getBody(), "UTF-8"));

                return response;
            }

Thanks

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Shailendra
  • 21
  • 2

1 Answers1

0

Cannot parse gzip encoded response with RestTemplate from Spring-Web

This was helpful to me for this same issue. You can try this out.

Ram
  • 191
  • 1
  • 2
  • 12