Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To send form-url-encoded data using Spring Cloud Feign's POST method, you need to follow these steps:

  1. Define an interface that extends the FeignClient interface and annotate it with @PostMapping to indicate that it is a POST method.

  2. Define the presence of one or more parameters and annotate them with @RequestParam to indicate that they are form parameters.

  3. Define a Map<String, ?> formParams parameter and annotate it with @RequestBody to indicate that it is the request body.

  4. In the implementation class, use Feign's @RequestLine annotation to define the HTTP request method and URL.

  5. Create an instance of the FormEncoder class to encode the form parameters and set it as the encoder for the Feign client.

  6. Use the interface to invoke the POST method.

Here is an example:

@FeignClient(name = "example-client", url = "http://example.com")
public interface ExampleClient {

   @PostMapping(value = "/foo")
   void post(@RequestParam("param1") String param1,
             @RequestParam("param2") String param2,
             @RequestBody Map<String, ?> formParams);

}

public class ExampleClientImpl implements ExampleClient {

   private final ExampleClient exampleClient;

   public ExampleClientImpl() {
      this.exampleClient = Feign.builder()
            .encoder(new FormEncoder(new JacksonEncoder()))
            .target(ExampleClient.class, "http://example.com");
   }

   @Override
   @RequestLine("POST /foo")
   public void post(String param1, String param2, Map<String, ?> formParams) {
      exampleClient.post(param1, param2, formParams);
   }

}