Skip to content

Commit 06286b1

Browse files
committed
Added URI variant methods to the RestTemplate.
1 parent 6aee6a1 commit 06286b1

File tree

3 files changed

+144
-9
lines changed

3 files changed

+144
-9
lines changed

org.springframework.web/src/main/java/org/springframework/web/client/RestOperations.java

+80-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ public interface RestOperations {
5757
*/
5858
<T> T getForObject(String url, Class<T> responseType, Map<String, String> uriVariables) throws RestClientException;
5959

60+
/**
61+
* Retrieve a representation by doing a GET on the URL . The response (if any) is converted and returned.
62+
*
63+
* @param url the URL
64+
* @param responseType the type of the return value
65+
* @return the converted object
66+
*/
67+
<T> T getForObject(URI url, Class<T> responseType) throws RestClientException;
68+
6069
// HEAD
6170

6271
/**
@@ -79,6 +88,14 @@ public interface RestOperations {
7988
*/
8089
HttpHeaders headForHeaders(String url, Map<String, String> uriVariables) throws RestClientException;
8190

91+
/**
92+
* Retrieve all headers of the resource specified by the URL.
93+
*
94+
* @param url the URL
95+
* @return all HTTP headers of that resource
96+
*/
97+
HttpHeaders headForHeaders(URI url) throws RestClientException;
98+
8299
// POST
83100

84101
/**
@@ -88,6 +105,7 @@ public interface RestOperations {
88105
*
89106
* @param url the URL
90107
* @param request the Object to be POSTed, may be <code>null</code>
108+
* @param uriVariables the variables to expand the template
91109
* @return the value for the <code>Location</code> header
92110
*/
93111
URI postForLocation(String url, Object request, String... uriVariables) throws RestClientException;
@@ -104,12 +122,23 @@ public interface RestOperations {
104122
*/
105123
URI postForLocation(String url, Object request, Map<String, String> uriVariables) throws RestClientException;
106124

125+
/**
126+
* Create a new resource by POSTing the given object to the URL, and returns the value of the
127+
* <code>Location</code> header. This header typically indicates where the new resource is stored.
128+
*
129+
* @param url the URL
130+
* @param request the Object to be POSTed, may be <code>null</code>
131+
* @return the value for the <code>Location</code> header
132+
*/
133+
URI postForLocation(URI url, Object request) throws RestClientException;
134+
107135
/**
108136
* Create a new resource by POSTing the given object to the URI template, and returns the representation
109137
* found in the response. <p>URI Template variables are expanded using the given URI variables, if any.
110138
*
111139
* @param url the URL
112140
* @param request the Object to be POSTed, may be <code>null</code>
141+
* @param uriVariables the variables to expand the template
113142
* @return the converted object
114143
*/
115144
<T> T postForObject(String url, Object request, Class<T> responseType, String... uriVariables)
@@ -121,11 +150,22 @@ <T> T postForObject(String url, Object request, Class<T> responseType, String...
121150
*
122151
* @param url the URL
123152
* @param request the Object to be POSTed, may be <code>null</code>
153+
* @param uriVariables the variables to expand the template
124154
* @return the converted object
125155
*/
126156
<T> T postForObject(String url, Object request, Class<T> responseType, Map<String, String> uriVariables)
127157
throws RestClientException;
128158

159+
/**
160+
* Create a new resource by POSTing the given object to the URL, and returns the representation
161+
* found in the response.
162+
*
163+
* @param url the URL
164+
* @param request the Object to be POSTed, may be <code>null</code>
165+
* @return the converted object
166+
*/
167+
<T> T postForObject(URI url, Object request, Class<T> responseType) throws RestClientException;
168+
129169
// PUT
130170

131171
/**
@@ -148,6 +188,14 @@ <T> T postForObject(String url, Object request, Class<T> responseType, Map<Strin
148188
*/
149189
void put(String url, Object request, Map<String, String> uriVariables) throws RestClientException;
150190

191+
/**
192+
* Creates a new resource by PUTting the given object to URL.
193+
*
194+
* @param url the URL
195+
* @param request the Object to be PUT, may be <code>null</code>
196+
*/
197+
void put(URI url, Object request) throws RestClientException;
198+
151199
// DELETE
152200

153201
/**
@@ -167,6 +215,13 @@ <T> T postForObject(String url, Object request, Class<T> responseType, Map<Strin
167215
*/
168216
void delete(String url, Map<String, String> uriVariables) throws RestClientException;
169217

218+
/**
219+
* Delete the resources at the specified URL.
220+
*
221+
* @param url the URL
222+
*/
223+
void delete(URI url) throws RestClientException;
224+
170225
// OPTIONS
171226

172227
/**
@@ -188,10 +243,18 @@ <T> T postForObject(String url, Object request, Class<T> responseType, Map<Strin
188243
*/
189244
Set<HttpMethod> optionsForAllow(String url, Map<String, String> uriVariables) throws RestClientException;
190245

246+
/**
247+
* Return the value of the Allow header for the given URL.
248+
*
249+
* @param url the URL
250+
* @return the value of the allow header
251+
*/
252+
Set<HttpMethod> optionsForAllow(URI url) throws RestClientException;
253+
191254
// general execution
192255

193256
/**
194-
* Execute the HTTP methods to the given URI, preparing the request with the {@link RequestCallback}, and reading the
257+
* Execute the HTTP methods to the given URI template, preparing the request with the {@link RequestCallback}, and reading the
195258
* response with a {@link ResponseExtractor}. <p>URI Template variables are expanded using the given URI variables, if
196259
* any.
197260
*
@@ -209,7 +272,7 @@ <T> T execute(String url,
209272
String... uriVariables) throws RestClientException;
210273

211274
/**
212-
* Execute the HTTP methods to the given URI, preparing the request with the {@link RequestCallback}, and reading the
275+
* Execute the HTTP methods to the given URI template, preparing the request with the {@link RequestCallback}, and reading the
213276
* response with a {@link ResponseExtractor}. <p>URI Template variables are expanded using the given URI variables
214277
* map.
215278
*
@@ -226,4 +289,19 @@ <T> T execute(String url,
226289
ResponseExtractor<T> responseExtractor,
227290
Map<String, String> uriVariables) throws RestClientException;
228291

292+
/**
293+
* Execute the HTTP methods to the given URL, preparing the request with the {@link RequestCallback}, and reading the
294+
* response with a {@link ResponseExtractor}.
295+
*
296+
* @param url the URL
297+
* @param method the HTTP method (GET, POST, etc)
298+
* @param requestCallback object that prepares the request
299+
* @param responseExtractor object that extracts the return value from the response
300+
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
301+
*/
302+
<T> T execute(URI url,
303+
HttpMethod method,
304+
RequestCallback requestCallback,
305+
ResponseExtractor<T> responseExtractor) throws RestClientException;
306+
229307
}

org.springframework.web/src/main/java/org/springframework/web/client/RestTemplate.java

+62-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.springframework.http.client.ClientHttpRequestFactory;
3232
import org.springframework.http.client.ClientHttpResponse;
3333
import org.springframework.http.client.support.HttpAccessor;
34-
import org.springframework.http.converter.BufferedImageHttpMessageConverter;
3534
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
3635
import org.springframework.http.converter.FormHttpMessageConverter;
3736
import org.springframework.http.converter.HttpMessageConverter;
@@ -41,7 +40,7 @@
4140
import org.springframework.web.util.UriTemplate;
4241

4342
/**
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
4544
* enforces RESTful principles. It handles HTTP connections, leaving application code to provide URLs (with possible
4645
* template variables) and extract results.
4746
*
@@ -55,9 +54,11 @@
5554
* <tr><td></td><td>{@link #postForObject}</td></tr>
5655
* <tr><td>PUT</td><td>{@link #put}</td></tr> <tr><td>any</td><td>{@link #execute}</td></tr> </table>
5756
*
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
6162
* <pre>
6263
* String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}", String.class,"42",
6364
* "21");
@@ -70,6 +71,8 @@
7071
* String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/rooms/{hotel}", String.class, vars);
7172
* </pre>
7273
* 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.
7376
*
7477
* <p>Objects passed to and returned from these methods are converted to and from HTTP messages by {@link
7578
* 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>
175178
new HttpMessageConverterExtractor<T>(responseType, supportedMessageConverters), urlVariables);
176179
}
177180

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+
178188
// HEAD
179189

180190
public HttpHeaders headForHeaders(String url, String... urlVariables) throws RestClientException {
@@ -185,6 +195,10 @@ public HttpHeaders headForHeaders(String url, Map<String, String> urlVariables)
185195
return execute(url, HttpMethod.HEAD, null, this.headersExtractor, urlVariables);
186196
}
187197

198+
public HttpHeaders headForHeaders(URI url) throws RestClientException {
199+
return execute(url, HttpMethod.HEAD, null, this.headersExtractor);
200+
}
201+
188202
// POST
189203

190204
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
206220
return headers.getLocation();
207221
}
208222

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+
209232
public <T> T postForObject(String url, Object request, Class<T> responseType, String... uriVariables)
210233
throws RestClientException {
211234
if (request != null) {
@@ -228,6 +251,16 @@ public <T> T postForObject(String url, Object request, Class<T> responseType, Ma
228251
new HttpMessageConverterExtractor<T>(responseType, responseMessageConverters), uriVariables);
229252
}
230253

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+
231264
// PUT
232265

233266
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
244277
execute(url, HttpMethod.PUT, new PostPutCallback(request), null, urlVariables);
245278
}
246279

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+
247287
// DELETE
248288

249289
public void delete(String url, String... urlVariables) throws RestClientException {
@@ -254,6 +294,10 @@ public void delete(String url, Map<String, String> urlVariables) throws RestClie
254294
execute(url, HttpMethod.DELETE, null, null, urlVariables);
255295
}
256296

297+
public void delete(URI url) throws RestClientException {
298+
execute(url, HttpMethod.DELETE, null, null);
299+
}
300+
257301
// OPTIONS
258302

259303
public Set<HttpMethod> optionsForAllow(String url, String... urlVariables) throws RestClientException {
@@ -268,6 +312,12 @@ public Set<HttpMethod> optionsForAllow(String url, Map<String, String> urlVariab
268312
return headers.getAllow();
269313
}
270314

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+
271321
// general execution
272322

273323
public <T> T execute(String url,
@@ -292,6 +342,13 @@ public <T> T execute(String url,
292342
return doExecute(expanded, method, requestCallback, responseExtractor);
293343
}
294344

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+
295352
/**
296353
* Execute the given method on the provided URI. The {@link ClientHttpRequest} is processed using the {@link
297354
* RequestCallback}; the response with the {@link ResponseExtractor}.

org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ public void serverError() {
114114
}
115115

116116
@Test
117-
public void optionsForAllow() {
118-
Set<HttpMethod> allowed = template.optionsForAllow("http://localhost:8889/get");
117+
public void optionsForAllow() throws URISyntaxException {
118+
Set<HttpMethod> allowed = template.optionsForAllow(new URI("http://localhost:8889/get"));
119119
assertEquals("Invalid response",
120120
EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS, HttpMethod.HEAD, HttpMethod.TRACE), allowed);
121121
}

0 commit comments

Comments
 (0)