31
31
import org .springframework .http .client .ClientHttpRequestFactory ;
32
32
import org .springframework .http .client .ClientHttpResponse ;
33
33
import org .springframework .http .client .support .HttpAccessor ;
34
- import org .springframework .http .converter .BufferedImageHttpMessageConverter ;
35
34
import org .springframework .http .converter .ByteArrayHttpMessageConverter ;
36
35
import org .springframework .http .converter .FormHttpMessageConverter ;
37
36
import org .springframework .http .converter .HttpMessageConverter ;
41
40
import org .springframework .web .util .UriTemplate ;
42
41
43
42
/**
44
- * <strong>The central class for client-side HTTP access.</strong>. It simplifies communication with HTTP servers, and
43
+ * <strong>The central class for client-side HTTP access.</strong> It simplifies communication with HTTP servers, and
45
44
* enforces RESTful principles. It handles HTTP connections, leaving application code to provide URLs (with possible
46
45
* template variables) and extract results.
47
46
*
55
54
* <tr><td></td><td>{@link #postForObject}</td></tr>
56
55
* <tr><td>PUT</td><td>{@link #put}</td></tr> <tr><td>any</td><td>{@link #execute}</td></tr> </table>
57
56
*
58
- * <p>Each of these methods takes {@linkplain UriTemplate uri template} arguments in two forms: as a {@code String}
59
- * variable arguments array, or as a {@code Map<String, String>}. The string varargs variant expands the given template
60
- * variables in order, so that
57
+ * <p>For each of these HTTP methods, there are three corresponding Java methods in the {@code RestTemplate}.
58
+ * Two variant take a {@code String} URI as first argument, and are capable of substituting any
59
+ * {@linkplain UriTemplate uri templates} in that URL using either a
60
+ * {@code String} variable arguments array, or a {@code Map<String, String>}. The string varargs variant expands the
61
+ * given template variables in order, so that
61
62
* <pre>
62
63
* String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}", String.class,"42",
63
64
* "21");
70
71
* String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/rooms/{hotel}", String.class, vars);
71
72
* </pre>
72
73
* will perform a GET on {@code http://example.com/hotels/42/rooms/42}.
74
+ * Alternatively, there are {@link URI} variant methods, which do not allow for URI templates, but allow you to reuse a
75
+ * single, expanded URI multiple times.
73
76
*
74
77
* <p>Objects passed to and returned from these methods are converted to and from HTTP messages by {@link
75
78
* HttpMessageConverter} instances. Converters for the main mime types are registered by default, but you can also write
@@ -175,6 +178,13 @@ public <T> T getForObject(String url, Class<T> responseType, Map<String, String>
175
178
new HttpMessageConverterExtractor <T >(responseType , supportedMessageConverters ), urlVariables );
176
179
}
177
180
181
+ public <T > T getForObject (URI url , Class <T > responseType ) throws RestClientException {
182
+ checkForSupportedMessageConverter (responseType );
183
+ List <HttpMessageConverter <T >> supportedMessageConverters = getSupportedMessageConverters (responseType );
184
+ return execute (url , HttpMethod .GET , new AcceptHeaderRequestCallback <T >(supportedMessageConverters ),
185
+ new HttpMessageConverterExtractor <T >(responseType , supportedMessageConverters ));
186
+ }
187
+
178
188
// HEAD
179
189
180
190
public HttpHeaders headForHeaders (String url , String ... urlVariables ) throws RestClientException {
@@ -185,6 +195,10 @@ public HttpHeaders headForHeaders(String url, Map<String, String> urlVariables)
185
195
return execute (url , HttpMethod .HEAD , null , this .headersExtractor , urlVariables );
186
196
}
187
197
198
+ public HttpHeaders headForHeaders (URI url ) throws RestClientException {
199
+ return execute (url , HttpMethod .HEAD , null , this .headersExtractor );
200
+ }
201
+
188
202
// POST
189
203
190
204
public URI postForLocation (String url , Object request , String ... urlVariables ) throws RestClientException {
@@ -206,6 +220,15 @@ public URI postForLocation(String url, Object request, Map<String, String> urlVa
206
220
return headers .getLocation ();
207
221
}
208
222
223
+ public URI postForLocation (URI url , Object request )
224
+ throws RestClientException {
225
+ if (request != null ) {
226
+ checkForSupportedMessageConverter (request .getClass ());
227
+ }
228
+ HttpHeaders headers = execute (url , HttpMethod .POST , new PostPutCallback (request ), this .headersExtractor );
229
+ return headers .getLocation ();
230
+ }
231
+
209
232
public <T > T postForObject (String url , Object request , Class <T > responseType , String ... uriVariables )
210
233
throws RestClientException {
211
234
if (request != null ) {
@@ -228,6 +251,16 @@ public <T> T postForObject(String url, Object request, Class<T> responseType, Ma
228
251
new HttpMessageConverterExtractor <T >(responseType , responseMessageConverters ), uriVariables );
229
252
}
230
253
254
+ public <T > T postForObject (URI url , Object request , Class <T > responseType ) throws RestClientException {
255
+ if (request != null ) {
256
+ checkForSupportedMessageConverter (request .getClass ());
257
+ }
258
+ checkForSupportedMessageConverter (responseType );
259
+ List <HttpMessageConverter <T >> responseMessageConverters = getSupportedMessageConverters (responseType );
260
+ return execute (url , HttpMethod .POST , new PostPutCallback <T >(request , responseMessageConverters ),
261
+ new HttpMessageConverterExtractor <T >(responseType , responseMessageConverters ));
262
+ }
263
+
231
264
// PUT
232
265
233
266
public void put (String url , Object request , String ... urlVariables ) throws RestClientException {
@@ -244,6 +277,13 @@ public void put(String url, Object request, Map<String, String> urlVariables) th
244
277
execute (url , HttpMethod .PUT , new PostPutCallback (request ), null , urlVariables );
245
278
}
246
279
280
+ public void put (URI url , Object request ) throws RestClientException {
281
+ if (request != null ) {
282
+ checkForSupportedMessageConverter (request .getClass ());
283
+ }
284
+ execute (url , HttpMethod .PUT , new PostPutCallback (request ), null );
285
+ }
286
+
247
287
// DELETE
248
288
249
289
public void delete (String url , String ... urlVariables ) throws RestClientException {
@@ -254,6 +294,10 @@ public void delete(String url, Map<String, String> urlVariables) throws RestClie
254
294
execute (url , HttpMethod .DELETE , null , null , urlVariables );
255
295
}
256
296
297
+ public void delete (URI url ) throws RestClientException {
298
+ execute (url , HttpMethod .DELETE , null , null );
299
+ }
300
+
257
301
// OPTIONS
258
302
259
303
public Set <HttpMethod > optionsForAllow (String url , String ... urlVariables ) throws RestClientException {
@@ -268,6 +312,12 @@ public Set<HttpMethod> optionsForAllow(String url, Map<String, String> urlVariab
268
312
return headers .getAllow ();
269
313
}
270
314
315
+ public Set <HttpMethod > optionsForAllow (URI url ) throws RestClientException {
316
+
317
+ HttpHeaders headers = execute (url , HttpMethod .OPTIONS , null , this .headersExtractor );
318
+ return headers .getAllow ();
319
+ }
320
+
271
321
// general execution
272
322
273
323
public <T > T execute (String url ,
@@ -292,6 +342,13 @@ public <T> T execute(String url,
292
342
return doExecute (expanded , method , requestCallback , responseExtractor );
293
343
}
294
344
345
+ public <T > T execute (URI url ,
346
+ HttpMethod method ,
347
+ RequestCallback requestCallback ,
348
+ ResponseExtractor <T > responseExtractor ) throws RestClientException {
349
+ return doExecute (url , method , requestCallback , responseExtractor );
350
+ }
351
+
295
352
/**
296
353
* Execute the given method on the provided URI. The {@link ClientHttpRequest} is processed using the {@link
297
354
* RequestCallback}; the response with the {@link ResponseExtractor}.
0 commit comments