What is RestTemplate?
RestTemplate is a template class in the Spring framework for accessing RESTful services. It provides a set of convenient methods to send HTTP requests and handle responses.
RestTemplate can be used to send GET, POST, PUT, DELETE and other types of requests, and can handle response data in various formats such as JSON and XML.
Using RestTemplate in Spring simplifies interaction with RESTful services, making it easier for developers to perform HTTP communication.
Below is an example code for sending a GET request using RestTemplate:
RestTemplate restTemplate = new RestTemplate();
String url = "https://api.example.com/data";
String response = restTemplate.getForObject(url, String.class);
System.out.println(response);
GET Methods
<T> T getForObject(String url, Class<T> responseType, Object... uriVariables);
<T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables);
<T> T getForObject(URI url, Class<T> responseType);
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables);
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables);
<T> ResponseEntity<T> getForEntity(URI var1, Class<T> responseType);
POST Methods
<T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables);
<T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables);
<T> T postForObject(URI url, @Nullable Object request, Class<T> responseType);
<T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables);
<T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables);
<T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType);
Commonly Used APIs
GET
1. getForObject
Returns an object converted from the response body, essentially the JSON.

Example: Retrieve payment info by primary key (remote call in microservices):
@GetMapping("/consumer/pay/get/{id}")
public ResultData getPayInfo(@PathVariable("id") Integer id) {
return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/" + id, ResultData.class, id);
}
Result:

2. getForEntity
Returns a ResponseEntity object containing response headers, status code, and body.

Usagee is similar to getForObject, but with additional response header information.
POST
1. postForObject
Example: Add order remotely:
/**
* Normally, typing a URL in the browser only sends GET requests.
* Underlying we call POST method, simulating a consumer sending GET request.
* Client consumer parameters may omit @RequestBody.
* @param payDTO
* @return
*/
@GetMapping("/consumer/pay/add")
public ResultData addOrder(PayDTO payDTO) {
return restTemplate.postForObject(PaymentSrv_URL + "/pay/add", payDTO, ResultData.class);
}
2. postForEntity
Similar to postForObject, but allows setting custom headers via HttpEntity.
The main difference between
postForObjectandpostForEntityis thatpostForEntityallows setting headers. UsepostForEntitywhen you need to specify header properties. Theexchangemethod is similar topostForEntitybut more flexible. It can also invoke GET, PUT, DELETE requests. When using these methods to send POST requests with parameters, the Map cannot be of the following two types (except when using placeholders in the URL):
Map<String, Object> paramMap = new HashMap<String, Object>();
Map<String, Object> paramMap = new LinkedHashMap<String, Object>();
3. exchange
Highly flexible and recommended.
It can invoke any HTTP method. Source code snippet:
// Source code from HttpMethod class (simplified)
public final class HttpMethod implements Comparable<HttpMethod>, Serializable {
public static final HttpMethod GET = new HttpMethod("GET");
public static final HttpMethod HEAD = new HttpMethod("HEAD");
public static final HttpMethod POST = new HttpMethod("POST");
public static final HttpMethod PUT = new HttpMethod("PUT");
public static final HttpMethod PATCH = new HttpMethod("PATCH");
public static final HttpMethod DELETE = new HttpMethod("DELETE");
public static final HttpMethod OPTIONS = new HttpMethod("OPTIONS");
public static final HttpMethod TRACE = new HttpMethod("TRACE");
// ...
}
As seen, common methods like PUT, POST, GET, DELETE are all included.
Example: Delete a record by primary key:
@DeleteMapping("/consumer/pay/del/{id}")
public Object delOrder(@PathVariable("id") Integer id) {
return restTemplate.exchange(
PaymentSrv_URL + "/pay/del/" + id,
HttpMethod.DELETE,
null,
ResultData.class
).getBody();
}
Postman result:

Very convenient...