Cheatsheet
Calling REST APIs
Key idea
`RestTemplate` and `RestClient` are Spring's synchronous HTTP clients: they build the request, convert objects to and from JSON with the same `HttpMessageConverter`s as Spring MVC and turn 4xx/5xx statuses into exceptions. `RestTemplate` has one named method per operation; `RestClient` (Spring 6.1+) a fluent API.
Diagram (UML)
classDiagram
direction LR
class WodClient {
<<@Component>>
-rest: RestClient
+today() Wod
+byName(String) Wod
}
class LegacyWodClient {
<<@Component>>
-rest: RestTemplate
+today() Wod
+publish(Wod) URI
}
class RestTemplate {
<<template>>
+getForObject(url, Class, vars) T
+getForEntity(url, Class, vars) ResponseEntity
+postForLocation(url, body) URI
+put(url, body, vars)
+delete(url, vars)
+exchange(url, method, entity, type) ResponseEntity
}
class RestClient {
<<fluent>>
+get() / post() / put() / delete()
+uri(...) retrieve() body(T)
}
class RestClientResponseException {
<<unchecked>>
}
WodClient --> RestClient
LegacyWodClient --> RestTemplate
RestTemplate ..> HttpMessageConverter
RestClient ..> HttpMessageConverter
RestClientResponseException <|-- HttpClientErrorException : 4xx
RestClientResponseException <|-- HttpServerErrorException : 5xxKey pieces
RestTemplate:getForObject,getForEntity,postForObject,postForEntity,postForLocation,put,delete,exchange,executeRestClient:get()/post()/put()/delete()→.uri(...)→.retrieve()→.body(T)/.toEntity(T)/.toBodilessEntity()ParameterizedTypeReference<List<Wod>>: generic types in the responseHttpClientErrorException(4xx),HttpServerErrorException(5xx), bothRestClientResponseExceptionRestTemplateBuilderandRestClient.Builder: built by Spring Boot, already configured@RestClientTest+MockRestServiceServer: test the client without a network
WodClient.java
public Wod today() {
return rest.get()
.uri("/wods/today")
.accept(MediaType.APPLICATION_JSON)
.retrieve() // send it and look at the response
.body(Wod.class); // JSON → Wod (4xx/5xx → RestClientResponseException)
}
public List<Wod> history() {
return rest.get().uri("/wods").retrieve()
.body(new ParameterizedTypeReference<List<Wod>>() { });
}
public URI publish(Wod wod) {
return rest.post().uri("/wods")
.contentType(MediaType.APPLICATION_JSON)
.body(wod) // Wod → JSON
.retrieve()
.toBodilessEntity()
.getHeaders().getLocation();
}Remember for the exam
getForObjectreturns only the body;getForEntity, aResponseEntitywith status and headerspostForLocationreturns theLocationheader (aURI)exchangeis the general method: verb,HttpEntity(body and headers) and response type- Template variables (
/wods/{name}) are filled from the extra arguments or aMap RestTemplateis in maintenance mode: it isn't removed, but the new options areRestClient(synchronous) orWebClient(reactive)- Inject Spring Boot's builder, don't call
new RestTemplate(): that way you inherit converters, timeouts and customizations
Exam traps
- With
RestTemplate, a 404 throwsHttpClientErrorException.NotFound; it doesn't returnnull - For
List<Wod>you needParameterizedTypeReference(or aWod[]array);List.classloses the generic type RestTemplate'sputanddeletereturnvoid; to read the response useexchange- With
rootUri/baseUrl,MockRestServiceServerexpectations use the relative path