 Java, Spring and Web Development tutorials  1. Introduction
HTTP requests are often the essence of web and API communication. They are the building blocks for protocols and proper data exchange.
In this tutorial, we’ll explore how to handle GET requests with a request body using Spring Cloud OpenFeign.
2. Understanding the Problem
According to the HTTP/1.1 specifications, a GET request shouldn’t contain a body:
- GET is meant to retrieve data (not send complex payloads).
- Most servers and proxies ignore or reject GET bodies.
- Caching layers like CDNs or browsers might misbehave when GET requests have a body.
However, some external APIs or legacy systems still expect a GET request with a body to pass complex search criteria. The challenge is that Spring Cloud OpenFeign, by default, doesn’t serialize a body for GET requests — it follows the HTTP standard strictly. Yet, with a few tweaks, it’s possible to handle such scenarios.
3. Project Setup
To use Feign, we need to add the OpenFeign dependency to the pom.xml file:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Next, we need to enable it in the main application class using the @EnableFeignClients annotation:
@SpringBootApplication
@EnableFeignClients
public class FeignDemoApplication {
public static void main(String[] args) {
SpringApplication.run(FeignDemoApplication.class, args);
}
}
This way, we can use the respective functionality.
4. Attempting a GET Request With a Body
Let’s start by seeing what happens if we naively try to send a GET request with a @RequestBody in Feign.
As an example, we first define a Feign client:
@FeignClient(name = "sampleClient", url = "http://localhost:8080")
public interface GetBodyFeignClient {
@GetMapping("/api/search")
String search(@RequestBody SearchRequest searchRequest);
}
Next, let’s define the request model:
public class SearchRequest {
private String keyword;
private String category;
// getters and setters
}
If we try to call this method in a test, we can see the result:
SearchRequest request = new SearchRequest();
request.setKeyword("spring");
request.setCategory("tutorial");
Assertions.assertThrows(FeignException.MethodNotAllowed.class, () -> {
getBodyFeignClient.search(request);
});
We get an exception:
feign.FeignException$MethodNotAllowed: status 405 reading SampleFeignClient#search(SearchRequest)
This happens because the presence of a request body in a GET method causes the underlying Feign infrastructure to construct a data object, which the server doesn’t expect.
By default, Feign and Spring MVC don’t allow a body in a GET request, and any attempt to send one is either ignored before the request is sent. Some servers reject such requests outright with an HTTP 400 or 405 error.
5. Using @SpringQueryMap to Fix the GET Request
To solve this issue, the correct approach is to use @SpringQueryMap instead of @RequestBody. This annotation tells Feign to serialize fields of a data object into URL query parameters, ensuring compliance with HTTP GET semantics and reliable operations across servers and proxies.
Thus, we can update the Feign client interface:
@FeignClient(name = "sampleClient", url = "http://localhost:8080")
public interface GetBodyFeignClient {
@GetMapping("/api/search")
String searchWithSpringQueryMap(@SpringQueryMap SearchRequest searchRequest);
}
Next, we call the relevant setters and perform the request:
SearchRequest request = new SearchRequest();
request.setKeyword("spring");
request.setCategory("tutorial");
getBodyFeignClient.searchWithSpringQueryMap(request);
Feign generates a GET request like this:
GET http://localhost:8080/api/search?keyword=spring&category=tutorial
Notably, the fields from the SearchRequest object are automatically converted to query parameters, eliminating the need for a body. This approach avoids the 405 Method Not Allowed or 400 Bad Request errors we might see if we had tried to send a body with a GET request. Importantly, complex data objects may not always be readily convertable in this manner, so it’s up to the developer to handle specific cases.
This integrated approach not only solves the problem but also ensures API calls are compatible with standard HTTP caching, proxy behavior, and server expectations, making them safer and more predictable.
6. Conclusion
In this article, we learned how to use@SpringQueryMap to handle GET requests that require multiple parameters or complex search criteria while staying fully compliant with the HTTP specification.
As always, the source code is available over on GitHub. The post Handling Feign GET Requests With a Body first appeared on Baeldung.
Content mobilized by FeedBlitz RSS Services, the premium FeedBurner alternative. |