@@ -30,7 +30,6 @@ import scala.reflect.ClassTag
30
30
* advantage of functions and Scala types.
31
31
*
32
32
* @author Arjen Poutsma
33
- * @author Ramnivas Laddad
34
33
* @since 1.0
35
34
* @constructor Creates a `RestTemplate` that wraps the given Java template, defaulting to the standard `RestTemplate`
36
35
* @param javaTemplate the Java `RestTemplate` to wrap
@@ -76,6 +75,17 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
76
75
Option (javaTemplate.getForObject(url, typeToClass[T ], uriVariables.asJava))
77
76
}
78
77
78
+ /**
79
+ * Retrieve a representation by doing a GET on the URL.
80
+ * The response (if any) is converted and returned.
81
+ *
82
+ * @param url the URL
83
+ * @return the converted object
84
+ */
85
+ def getForAny [T : ClassTag ](url : URI ): Option [T ] = {
86
+ Option (javaTemplate.getForObject(url, typeToClass[T ]))
87
+ }
88
+
79
89
/**
80
90
* Retrieve an entity by doing a GET on the specified URL.
81
91
* The response is converted and stored in an `ResponseEntity`.
@@ -102,6 +112,15 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
102
112
javaTemplate.getForEntity(url, typeToClass[T ], uriVariables.asJava)
103
113
}
104
114
115
+ /**
116
+ * Retrieve a representation by doing a GET on the URL .
117
+ * The response is converted and stored in an [[org.springframework.http.ResponseEntity ]]}.
118
+ * @param url the URL
119
+ * @return the converted object
120
+ */
121
+ def getForEntity [T : ClassTag ](url : URI ): ResponseEntity [T ] = {
122
+ javaTemplate.getForEntity(url, typeToClass[T ])
123
+ }
105
124
106
125
// HEAD
107
126
/**
@@ -130,6 +149,14 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
130
149
javaTemplate.headForHeaders(url, uriVariables.asJava)
131
150
}
132
151
152
+ /**
153
+ * Retrieve all headers of the resource specified by the URL.
154
+ * @param url the URL
155
+ * @return all HTTP headers of that resource
156
+ */
157
+ def headForHeaders (url : URI ): HttpHeaders = {
158
+ javaTemplate.headForHeaders(url)
159
+ }
133
160
134
161
// POST
135
162
/**
@@ -172,6 +199,22 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
172
199
javaTemplate.postForLocation(url, request.orNull, uriVariables.asJava)
173
200
}
174
201
202
+ /**
203
+ * Create a new resource by POSTing the given object to the URL, and returns the value of the `Location` header.
204
+ * This header typically indicates where the new resource is stored.
205
+ *
206
+ * The `request` parameter can be a [[org.springframework.http.HttpEntity ]] in order to add additional HTTP headers
207
+ * to the request.
208
+ *
209
+ * @param url the URL
210
+ * @param request the Object to be POSTed
211
+ * @return the value for the `Location` header
212
+ * @see HttpEntity
213
+ */
214
+ def postForLocation (url : URI , request : Option [Any ]): URI = {
215
+ javaTemplate.postForLocation(url, request.orNull)
216
+ }
217
+
175
218
/**
176
219
* Create a new resource by POSTing the given object to the URI template, and returns the representation found in
177
220
* the response.
@@ -210,6 +253,22 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
210
253
Option (javaTemplate.postForObject(url, request.orNull, typeToClass[T ], uriVariables.asJava))
211
254
}
212
255
256
+ /**
257
+ * Create a new resource by POSTing the given object to the URL, and returns the representation found in the
258
+ * response.
259
+ *
260
+ * The `request` parameter can be a [[org.springframework.http.HttpEntity ]] in order to add additional HTTP headers
261
+ * to the request.
262
+ *
263
+ * @param url the URL
264
+ * @param request the Object to be POSTed
265
+ * @return the converted object
266
+ * @see HttpEntity
267
+ */
268
+ def postForObject [T : ClassTag ](url : URI , request : Option [Any ]): Option [T ] = {
269
+ Option (javaTemplate.postForObject(url, request.orNull, typeToClass[T ]))
270
+ }
271
+
213
272
/**
214
273
* Create a new resource by POSTing the given object to the URI template, and returns the response as
215
274
* [[org.springframework.http.ResponseEntity ]].
@@ -248,6 +307,23 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
248
307
javaTemplate.postForEntity(url, request.orNull, typeToClass[T ], uriVariables.asJava)
249
308
}
250
309
310
+ /**
311
+ * Create a new resource by POSTing the given object to the URL, and returns the response as
312
+ * [[org.springframework.http.ResponseEntity ]].
313
+ *
314
+ * The `request` parameter can be a [[org.springframework.http.HttpEntity ]] in order to add additional HTTP headers
315
+ * to the request.
316
+ *
317
+ * @param url the URL
318
+ * @param request the Object to be POSTed, may be `null`
319
+ * @return the converted object
320
+ * @see HttpEntity
321
+ * @since 3.0.2
322
+ */
323
+ def postForEntity [T : ClassTag ](url : URI , request : Option [Any ]): ResponseEntity [T ] = {
324
+ javaTemplate.postForEntity(url, request.orNull, typeToClass[T ])
325
+ }
326
+
251
327
// PUT
252
328
/**
253
329
* Create or update a resource by PUTting the given object to the URI.
@@ -283,6 +359,19 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
283
359
javaTemplate.put(url, request.orNull, uriVariables.asJava)
284
360
}
285
361
362
+ /**
363
+ * Creates a new resource by PUTting the given object to URL.
364
+ *
365
+ * The `request` parameter can be a [[org.springframework.http.HttpEntity ]] in order to add additional HTTP headers
366
+ * to the request.
367
+ *
368
+ * @param url the URL
369
+ * @param request the Object to be PUT
370
+ * @see HttpEntity
371
+ */
372
+ def put (url : URI , request : Option [Any ]) {
373
+ javaTemplate.put(url, request.orNull)
374
+ }
286
375
287
376
// DELETE
288
377
/**
@@ -308,6 +397,14 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
308
397
javaTemplate.delete(url, uriVariables.asJava)
309
398
}
310
399
400
+ /**
401
+ * Delete the resources at the specified URL.
402
+ *
403
+ * @param url the URL
404
+ */
405
+ def delete (url : URI ) {
406
+ javaTemplate.delete(url)
407
+ }
311
408
312
409
// OPTIONS
313
410
/**
@@ -336,6 +433,16 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
336
433
javaTemplate.optionsForAllow(url, uriVariables.asJava).asScala
337
434
}
338
435
436
+ /**
437
+ * Return the value of the Allow header for the given URL.
438
+ *
439
+ * @param url the URL
440
+ * @return the value of the allow header
441
+ */
442
+ def optionsForAllow (url : URI ): Set [HttpMethod ] = {
443
+ javaTemplate.optionsForAllow(url).asScala
444
+ }
445
+
339
446
// exchange
340
447
/**
341
448
* Execute the HTTP method to the given URI template, writing the given request entity to the request, and
@@ -370,6 +477,18 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
370
477
javaTemplate.exchange(url, method, requestEntity.orNull, typeToClass[T ], uriVariables.asJava)
371
478
}
372
479
480
+ /**
481
+ * Execute the HTTP method to the given URI template, writing the given request entity to the request, and
482
+ * returns the response as `ResponseEntity`.
483
+ *
484
+ * @param url the URL
485
+ * @param method the HTTP method (GET, POST, etc)
486
+ * @param requestEntity the entity (headers and/or body) to write to the request
487
+ * @return the response as entity
488
+ */
489
+ def exchange [T : ClassTag ](url : URI , method : HttpMethod , requestEntity : Option [HttpEntity [_]]): ResponseEntity [T ] = {
490
+ javaTemplate.exchange(url, method, requestEntity.orNull, typeToClass[T ])
491
+ }
373
492
374
493
// general execution
375
494
/**
@@ -414,6 +533,24 @@ class RestTemplate(val javaTemplate: org.springframework.web.client.RestOperatio
414
533
uriVariables.asJava))
415
534
}
416
535
536
+ /**
537
+ * Execute the HTTP method to the given URL, preparing the request with the given function, and reading the response
538
+ * with a function.
539
+ *
540
+ * @param url the URL
541
+ * @param method the HTTP method (GET, POST, etc)
542
+ * @param requestFunction function that prepares the request
543
+ * @param responseFunction function that extracts the return value from the response
544
+ * @return an arbitrary object, as returned by the response object
545
+ */
546
+ def execute [T ](url : URI , method : HttpMethod )
547
+ (requestFunction : ClientHttpRequest => Unit )
548
+ (responseFunction : ClientHttpResponse => T ): Option [T ] = {
549
+ Option (javaTemplate.execute(url, method, functionToRequestCallback(requestFunction),
550
+ functionToResponseExtractor(responseFunction)))
551
+
552
+ }
553
+
417
554
private def asInstanceOfAnyRef (seq : Seq [Any ]) = {
418
555
seq.map(_.asInstanceOf [AnyRef ])
419
556
}
0 commit comments