SPR-5866 - RestTemplate - access to Request Headers
This commit is contained in:
parent
886eb665bf
commit
b0e3081636
|
|
@ -587,13 +587,22 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
|
||||||
private final HttpMessageConverterExtractor<T> delegate;
|
private final HttpMessageConverterExtractor<T> delegate;
|
||||||
|
|
||||||
public HttpEntityResponseExtractor(Class<T> responseType) {
|
public HttpEntityResponseExtractor(Class<T> responseType) {
|
||||||
|
if (responseType != null) {
|
||||||
this.delegate = new HttpMessageConverterExtractor<T>(responseType, getMessageConverters());
|
this.delegate = new HttpMessageConverterExtractor<T>(responseType, getMessageConverters());
|
||||||
|
} else {
|
||||||
|
this.delegate = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpEntity<T> extractData(ClientHttpResponse response) throws IOException {
|
public HttpEntity<T> extractData(ClientHttpResponse response) throws IOException {
|
||||||
|
if (delegate != null) {
|
||||||
T body = delegate.extractData(response);
|
T body = delegate.extractData(response);
|
||||||
return new HttpEntity<T>(body, response.getHeaders());
|
return new HttpEntity<T>(body, response.getHeaders());
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
return new HttpEntity<T>(response.getHeaders());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,9 +50,9 @@ import org.mortbay.jetty.servlet.ServletHolder;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.client.CommonsClientHttpRequestFactory;
|
import org.springframework.http.client.CommonsClientHttpRequestFactory;
|
||||||
import org.springframework.util.FileCopyUtils;
|
import org.springframework.util.FileCopyUtils;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
|
|
@ -179,7 +179,7 @@ public class RestTemplateIntegrationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void exchange() throws Exception {
|
public void exchangeGet() throws Exception {
|
||||||
HttpHeaders requestHeaders = new HttpHeaders();
|
HttpHeaders requestHeaders = new HttpHeaders();
|
||||||
requestHeaders.set("MyHeader", "MyValue");
|
requestHeaders.set("MyHeader", "MyValue");
|
||||||
HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
|
HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
|
||||||
|
|
@ -188,6 +188,15 @@ public class RestTemplateIntegrationTests {
|
||||||
assertEquals("Invalid content", helloWorld, response.getBody());
|
assertEquals("Invalid content", helloWorld, response.getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void exchangePost() throws Exception {
|
||||||
|
HttpHeaders requestHeaders = new HttpHeaders();
|
||||||
|
requestHeaders.set("MyHeader", "MyValue");
|
||||||
|
HttpEntity<String> requestEntity = new HttpEntity<String>(helloWorld, requestHeaders);
|
||||||
|
HttpEntity<?> result = template.exchange(URI + "/{method}", HttpMethod.POST, requestEntity, null, "post");
|
||||||
|
assertEquals("Invalid location", new URI(URI + "/post/1"), result.getHeaders().getLocation());
|
||||||
|
assertFalse(result.hasBody());
|
||||||
|
}
|
||||||
|
|
||||||
/** Servlet that returns and error message for a given status code. */
|
/** Servlet that returns and error message for a given status code. */
|
||||||
private static class ErrorServlet extends GenericServlet {
|
private static class ErrorServlet extends GenericServlet {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue