Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Feign Client + Eureka POST request body

I'm trying to use Feign and Eureka to forward a post request from server A to server B. Both servers are discrovered sucessfully by Eureka.

This works:

@Feignclient
public interface MyFeignClient {
    @RequestMapping(value = "test", = RequestMethod.POST, consumes = "application/json")
    ResponseEntity<String> theActualMethod(
            HttpServletRequest request,
            @RequestHeader("firstHeader") String header1,
            @RequestHeader("secondHeader") byte[] header2);
}

However, when I change the second argument to @RequestBody in order to read the POST request content, I get an exception:

java.lang.IllegalStateException: Method has too many Body parameters: public abstract org.springframework.http.ResponseEntity MyFeignClient.theActualMethod(javax.servlet.http.HttpServletRequest,java.lang.String,byte[])
like image 917
Yoaz Menda Avatar asked Feb 08 '16 07:02

Yoaz Menda


People also ask

How do you pass a body request in feign client?

Passing arguments through the request body of HTTP requests This will instruct Feign to issue a PUT call to the service, adding the query paramter “iterations” to the request URL and the string value in the HTTP request body. String postToServerOnSecondService(String value, @RequestParam int id);

How do I use feign client with Eureka?

To use it, we simply annotate an interface with @FeignClient(“service-name”) and auto-wire it into a controller. A good method for creating such Feign Clients is to create interfaces with @RequestMapping annotated methods and put them into a separate module. This way they can be shared between server and client.

Is FeignClient deprecated?

Feign client is really convenient tool to use. But I recently came to know that Rest-Template is going to be deprecated and will be replaced by WebClient, and Feign Client internally uses Rest-Template.

Is feign client better than RestTemplate?

One of the advantages of using Feign over RestTemplate is that, we do not need to write any implementation to call the other services. So there is no need to write any unit test as there is no code to test in the first place.


2 Answers

The problem was that a method in Feign interface cannot have more than one 'general' argument. you can have as many header arguments as you want but not more than one as body. Since @RequestBody doesn't do anything it is regarded not as a header but as another variable in addition to the HttpServletRequest request variable.

So I had to change my business logic to have only one parameter.

like image 136
Yoaz Menda Avatar answered Oct 12 '22 21:10

Yoaz Menda


For me, the issue was that I used @Param (as in feign.Param) instead of @RequestParam (as in org.springframework.web.bind.annotation.RequestParam). Changing all @Param to @RequestParam solved it for me.

I Don't know why this is but a related question on Feign's repository might explain a little.

like image 37
asherbret Avatar answered Oct 12 '22 22:10

asherbret