Languages

Spring Boot 3 - How can I perform client API GET request with request body using RestTemplate?

3 points
Asked by:
Roaming_Hot_Dong
1060

I want to perform a GET request with body.

How to do that or what library should I use?

I am currently using Java 17, Spring Boot 3.0.1 and RestTemplate.

 

1 answer
7 points
Answered by:
Roaming_Hot_Dong
1060

The solution for your problem is to extend existing RestTemplate class.

RestTemplate class provides execute() method that can be used to perform GET request with body.

I the below you can find practical example and ExtendedRestTemplate class that contains required API extension for your problem - GET method + Payload/Body.

 

Practical example

Example ExtendedRestTemplate class usage:

ExtendedRestTemplate restTemplate = new ExtendedRestTemplate();

SomeRequest someRequest = new SomeRequest(1L, "John", "john@email.com");
SomeResponse someResponse = restTemplate.getForObject("http://localhost/api/some-path", someRequest, SomeResponse.class);  // <--- GET method request with body

 

Example ExtendedRestTemplate.java file:

package com.dirask.example;

import java.net.URI;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.lang.Nullable;
import org.springframework.web.client.HttpMessageConverterExtractor;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

public class ExtendedRestTemplate extends RestTemplate {

	public ExtendedRestTemplate() {
		super();
	}

	public ExtendedRestTemplate(ClientHttpRequestFactory requestFactory) {
		super(requestFactory);
	}

	public ExtendedRestTemplate(List<HttpMessageConverter<?>> messageConverters) {
		super(messageConverters);
	}

	@Nullable
	public <T> T getForObject(URI url, @Nullable Object requestObject, Class<T> responseType) throws RestClientException {
		List<HttpMessageConverter<?>> messageConverters = super.getMessageConverters();
		RequestCallback requestCallback = super.httpEntityCallback(requestObject, responseType);
		HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor<>(responseType, messageConverters);
		return super.execute(url, HttpMethod.GET, requestCallback, responseExtractor);
	}

	@Nullable
	public <T> T getForObject(String url, @Nullable Object requestObject, Class<T> responseType, Object... uriVariables) throws RestClientException {
		List<HttpMessageConverter<?>> messageConverters = super.getMessageConverters();
		RequestCallback requestCallback = httpEntityCallback(requestObject, responseType);
		HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor<>(responseType, messageConverters);
		return super.execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
	}

	@Nullable
	public <T> T getForObject(String url, @Nullable Object requestObject, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
		List<HttpMessageConverter<?>> messageConverters = super.getMessageConverters();
		RequestCallback requestCallback = super.httpEntityCallback(requestObject, responseType);
		HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor<>(responseType, messageConverters);
		return super.execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
	}
}

 

See also

  1. Spring Boot 3 - RestTemplate and GET method request with body (JSON payload)

  2. Spring Boot 3 - RestTemplate and GET method request with body (JSON payload)

0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join