Spring RestTemplate Behavior when handling responses with a status of NO_CONTENT

前端 未结 6 1708
小蘑菇
小蘑菇 2020-12-08 11:05

Okay, I have a class NamedSystems, that has as its only field a Set of NamedSystem.

I have a method to find NamedSystems by certain criteria. That\'s not really imp

6条回答
  •  庸人自扰
    2020-12-08 11:41

    Or you could extend RestTemplate and override doExecute(..) and check the response body.

    For example here is what I implemented and works for us:

    @Override
    protected  T doExecute(final URI url, final HttpMethod method, final RequestCallback requestCallback, final ResponseExtractor responseExtractor)
            throws RestClientException
    {
        Assert.notNull(url, "'url' must not be null");
        Assert.notNull(method, "'method' must not be null");
        ClientHttpResponse response = null;
        try
        {
            final ClientHttpRequest request = createRequest(url, method);
            if (requestCallback != null)
            {
                requestCallback.doWithRequest(request);
            }
            response = request.execute();
            if (!getErrorHandler().hasError(response))
            {
                logResponseStatus(method, url, response);
            }
            else
            {
                handleResponseError(method, url, response);
            }
            if ((response.getBody() == null) || (responseExtractor == null))
            {
                return null;
            }
            return responseExtractor.extractData(response);
        }
        catch (final IOException ex)
        {
            throw new ResourceAccessException("I/O error: " + ex.getMessage(), ex);
        }
        finally
        {
            if (response != null)
            {
                response.close();
            }
        }
    }
    

提交回复
热议问题