Public RequestCallback/ResponseExtractor factory methods
Issue: SPR-8604
This commit is contained in:
parent
eef592d901
commit
79e809be24
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
|
|
@ -17,14 +17,21 @@
|
|||
package org.springframework.web.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.springframework.http.client.ClientHttpRequest;
|
||||
|
||||
/**
|
||||
* Callback interface for code that operates on a {@link ClientHttpRequest}. Allows to manipulate the request
|
||||
* headers, and write to the request body.
|
||||
* Callback interface for code that operates on a {@link ClientHttpRequest}.
|
||||
* Allows manipulating the request headers, and write to the request body.
|
||||
*
|
||||
* <p>Used internally by the {@link RestTemplate}, but also useful for application code.
|
||||
* <p>Used internally by the {@link RestTemplate}, but also useful for
|
||||
* application code. There several available factory methods:
|
||||
* <ul>
|
||||
* <li>{@link RestTemplate#acceptHeaderRequestCallback(Class)}
|
||||
* <li>{@link RestTemplate#httpEntityCallback(Object)}
|
||||
* <li>{@link RestTemplate#httpEntityCallback(Object, Type)}
|
||||
* </ul>
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see RestTemplate#execute
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
|
|
@ -17,6 +17,7 @@
|
|||
package org.springframework.web.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
|
@ -27,7 +28,9 @@ import org.springframework.lang.Nullable;
|
|||
* from a {@link ClientHttpResponse}, but don't need to worry about exception
|
||||
* handling or closing resources.
|
||||
*
|
||||
* <p>Used internally by the {@link RestTemplate}, but also useful for application code.
|
||||
* <p>Used internally by the {@link RestTemplate}, but also useful for
|
||||
* application code. There is one available factory method, see
|
||||
* {@link RestTemplate#responseEntityExtractor(Type)}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.0
|
||||
|
|
|
|||
|
|
@ -673,6 +673,17 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
|
|||
|
||||
// general execution
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>To provide a {@code RequestCallback} or {@code ResponseExtractor} only,
|
||||
* but not both, consider using:
|
||||
* <ul>
|
||||
* <li>{@link #acceptHeaderRequestCallback(Class)}
|
||||
* <li>{@link #httpEntityCallback(Object)}
|
||||
* <li>{@link #httpEntityCallback(Object, Type)}
|
||||
* <li>{@link #responseEntityExtractor(Type)}
|
||||
* </ul>
|
||||
*/
|
||||
@Override
|
||||
@Nullable
|
||||
public <T> T execute(String url, HttpMethod method, @Nullable RequestCallback requestCallback,
|
||||
|
|
@ -682,6 +693,17 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
|
|||
return doExecute(expanded, method, requestCallback, responseExtractor);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>To provide a {@code RequestCallback} or {@code ResponseExtractor} only,
|
||||
* but not both, consider using:
|
||||
* <ul>
|
||||
* <li>{@link #acceptHeaderRequestCallback(Class)}
|
||||
* <li>{@link #httpEntityCallback(Object)}
|
||||
* <li>{@link #httpEntityCallback(Object, Type)}
|
||||
* <li>{@link #responseEntityExtractor(Type)}
|
||||
* </ul>
|
||||
*/
|
||||
@Override
|
||||
@Nullable
|
||||
public <T> T execute(String url, HttpMethod method, @Nullable RequestCallback requestCallback,
|
||||
|
|
@ -692,6 +714,17 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
|
|||
return doExecute(expanded, method, requestCallback, responseExtractor);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>To provide a {@code RequestCallback} or {@code ResponseExtractor} only,
|
||||
* but not both, consider using:
|
||||
* <ul>
|
||||
* <li>{@link #acceptHeaderRequestCallback(Class)}
|
||||
* <li>{@link #httpEntityCallback(Object)}
|
||||
* <li>{@link #httpEntityCallback(Object, Type)}
|
||||
* <li>{@link #responseEntityExtractor(Type)}
|
||||
* </ul>
|
||||
*/
|
||||
@Override
|
||||
@Nullable
|
||||
public <T> T execute(URI url, HttpMethod method, @Nullable RequestCallback requestCallback,
|
||||
|
|
@ -770,34 +803,38 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns a request callback implementation that prepares the request {@code Accept}
|
||||
* headers based on the given response type and configured
|
||||
* {@linkplain #getMessageConverters() message converters}.
|
||||
* Return a {@code RequestCallback} that sets the request {@code Accept}
|
||||
* header based on the given response type, cross-checked against the
|
||||
* configured message converters.
|
||||
*/
|
||||
protected <T> RequestCallback acceptHeaderRequestCallback(Class<T> responseType) {
|
||||
public <T> RequestCallback acceptHeaderRequestCallback(Class<T> responseType) {
|
||||
return new AcceptHeaderRequestCallback(responseType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a request callback implementation that writes the given object to the
|
||||
* request stream.
|
||||
* Return a {@code RequestCallback} implementation that writes the given
|
||||
* object to the request stream.
|
||||
*/
|
||||
protected <T> RequestCallback httpEntityCallback(@Nullable Object requestBody) {
|
||||
public <T> RequestCallback httpEntityCallback(@Nullable Object requestBody) {
|
||||
return new HttpEntityRequestCallback(requestBody);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a request callback implementation that writes the given object to the
|
||||
* request stream.
|
||||
* Return a {@code RequestCallback} implementation that:
|
||||
* <ol>
|
||||
* <li>Sets the request {@code Accept} header based on the given response
|
||||
* type, cross-checked against the configured message converters.
|
||||
* <li>Writes the given object to the request stream.
|
||||
* </ol>
|
||||
*/
|
||||
protected <T> RequestCallback httpEntityCallback(@Nullable Object requestBody, Type responseType) {
|
||||
public <T> RequestCallback httpEntityCallback(@Nullable Object requestBody, Type responseType) {
|
||||
return new HttpEntityRequestCallback(requestBody, responseType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a response extractor for {@link ResponseEntity}.
|
||||
* Return a {@code ResponseExtractor} that prepares a {@link ResponseEntity}.
|
||||
*/
|
||||
protected <T> ResponseExtractor<ResponseEntity<T>> responseEntityExtractor(Type responseType) {
|
||||
public <T> ResponseExtractor<ResponseEntity<T>> responseEntityExtractor(Type responseType) {
|
||||
return new ResponseEntityResponseExtractor<>(responseType);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue