SPR-7911 - Better handling of 204 No Content in RestTemplate
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,6 +22,7 @@ import java.util.List;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
@@ -61,9 +62,15 @@ public class HttpMessageConverterExtractor<T> implements ResponseExtractor<T> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T extractData(ClientHttpResponse response) throws IOException {
|
||||
if (!hasMessageBody(response)) {
|
||||
return null;
|
||||
}
|
||||
MediaType contentType = response.getHeaders().getContentType();
|
||||
if (contentType == null) {
|
||||
throw new RestClientException("Cannot extract response: no Content-Type found");
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("No Content-Type header found, defaulting to application/octet-stream");
|
||||
}
|
||||
contentType = MediaType.APPLICATION_OCTET_STREAM;
|
||||
}
|
||||
for (HttpMessageConverter messageConverter : messageConverters) {
|
||||
if (messageConverter.canRead(responseType, contentType)) {
|
||||
@@ -79,4 +86,22 @@ public class HttpMessageConverterExtractor<T> implements ResponseExtractor<T> {
|
||||
this.responseType.getName() + "] and content type [" + contentType + "]");
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether the given response has a message body.
|
||||
* <p>Default implementation returns {@code false} for a response status of {@code 204} or {@code 304}, or a
|
||||
* {@code Content-Length} of {@code 0}.
|
||||
*
|
||||
* @param response the response to check for a message body
|
||||
* @return {@code true} if the response has a body, {@code false} otherwise
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
protected boolean hasMessageBody(ClientHttpResponse response) throws IOException {
|
||||
HttpStatus responseStatus = response.getStatusCode();
|
||||
if (responseStatus == HttpStatus.NO_CONTENT || responseStatus == HttpStatus.NOT_MODIFIED) {
|
||||
return false;
|
||||
}
|
||||
long contentLength = response.getHeaders().getContentLength();
|
||||
return contentLength != 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user