VOOZH about

URL: https://www.javacodegeeks.com/feign-http-get-request-body-example.html

⇱ Feign HTTP GET Request Body Example - Java Code Geeks


Feign is a declarative HTTP client in the Spring ecosystem that simplifies calling external APIs. While Feign makes it easy to define REST clients, one common issue we face is attempting to send a GET request with a request body, a pattern that is technically allowed by HTTP but not widely supported by servers. In this article, we will explore how to handle this scenario in a Spring Boot application using Feign.

1. Project Setup

We’ll set up a simple Spring Boot project that includes Feign support.

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>

We include spring-cloud-starter-openfeign for Feign support.

Main Application Class

@SpringBootApplication
@EnableFeignClients
public class FeignGetBodyApplication {

	public static void main(String[] args) {
		SpringApplication.run(FeignGetBodyApplication.class, args);
	}

}

@EnableFeignClients scans for Feign client interfaces in the application. This is required for Spring to generate proxy implementations at runtime.

2. Trying to Send a GET Request With a Body

HTTP/1.1 specification (RFC 7231) technically allows GET requests to contain a body, but many servers and clients ignore or reject it. If we attempt this directly with Feign, we’ll encounter issues.

Example Feign Client (Problematic Approach)

@FeignClient(name = "exampleClient", url = "http://localhost:8080")
public interface ExampleClient {

 @GetMapping("/filter")
 String filterWithBody(@RequestBody FilterCriteria criteria);
}
class FilterCriteria {

 private String category;
 private String type;

 public String getCategory() {
 return category;
 }

 public void setCategory(String category) {
 this.category = category;
 }

 public String getType() {
 return type;
 }

 public void setType(String type) {
 this.type = type;
 }
}

Here, we attempt to send a FilterCriteria object in the body of a GET request. When you run this code, Feign throws an exception or the server ignores the request body because GET requests are generally not designed to include one.

To demonstrate this, we’ll create a runner class that triggers the Feign client call.

@Component
public class FeignTestRunner implements CommandLineRunner {

 private static final Logger logger = LoggerFactory.getLogger(FeignTestRunner.class);
 private final ExampleClient exampleClient;

 public FeignTestRunner(ExampleClient exampleClient) {
 this.exampleClient = exampleClient;
 }

 @Override
 public void run(String... args) {
 FilterCriteria criteria = new FilterCriteria();
 criteria.setCategory("Books");
 criteria.setType("Fiction");

 try {
 String response = exampleClient.filterWithBody(criteria);
 logger.info("Response: {}", response);
 } catch (Exception ex) {
 logger.error("Error occurred while calling Feign client: {}", ex.getMessage());
 }
 }
}

When you run the application, Feign will attempt to send a GET request with a request body. Most servers and Feign itself will not support this properly, resulting in an exception similar to the following:

: Error occurred while calling Feign client: [405] during [GET] to [http://localhost:8080/filter] 
[{"timestamp":"2025-11-13T18:36:22.481+00:00","status":405,"error":"Method Not Allowed","path":"/filter"}]

The error confirms that the HTTP GET method does not support sending a body with the request.

3. Using @SpringQueryMap to Fix the GET Request

Spring Cloud OpenFeign provides @SpringQueryMap, which maps object properties to query parameters. This approach allows us to simulate sending a “body” for a GET request without breaking HTTP conventions.

Fixed Feign Client

@FeignClient(name = "exampleClient", url = "http://localhost:8080")
public interface ExampleClient {

 @GetMapping("/filter")
 String filterWithBody(@SpringQueryMap FilterCriteria criteria);
}

Now, calling this client with a FilterCriteria object like:

 @Override
 public void run(String... args) {
 FilterCriteria criteria = new FilterCriteria();
 criteria.setCategory("Books");
 criteria.setType("Fiction");

 try {
 String response = exampleClient.filterWithBody(criteria);
 logger.info("Response: {}", response);
 } catch (Exception ex) {
 logger.error("Error occurred while calling Feign client: {}", ex.getMessage());
 }
 }

Will result in a proper GET request.

GET /filter?category=Books&type=Fiction

Example Controller Endpoint

@RestController
public class FilterController {

 @GetMapping("/filter")
 public String filterItems(@RequestParam String category, @RequestParam String type) {
 return "Filtered items in category: " + category + ", type: " + type;
 }
}

Logged Output:

com.jcg.example.FeignTestRunner: Response: Filtered items in category: Books, type: Fiction

The FilterCriteria object is converted into query parameters using @SpringQueryMap, allowing the controller to receive them as expected and return a response, which avoids the issues of sending a GET request body and ensures full compatibility with HTTP standards.

4. Conclusion

In this article, we explored how to handle GET requests with structured data using Feign in Spring Boot. We saw that sending a body in a GET request is not reliably supported and can lead to errors, such as method not allowed exceptions. To address this, we demonstrated how to use @SpringQueryMap to convert an object’s fields into query parameters, enabling the Feign client to communicate properly with the server.

5. Download the Source Code

This article explored how to handle an HTTP GET request with a body when using Feign.

Download
You can download the full source code of this example here: feign http get request body
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Omozegie Aziegbe
Omozegie Aziegbe
November 20th, 2025Last Updated: November 20th, 2025
0 535 3 minutes read

Omozegie Aziegbe

Omos Aziegbe is a technical writer and web/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz