To send form-url-encoded data using Spring Cloud Feign's POST method, you need to follow these steps:
Define an interface that extends the FeignClient
interface and annotate it with @PostMapping
to indicate that it is a POST method.
Define the presence of one or more parameters and annotate them with @RequestParam
to indicate that they are form parameters.
Define a Map<String, ?> formParams
parameter and annotate it with @RequestBody
to indicate that it is the request body.
In the implementation class, use Feign's @RequestLine
annotation to define the HTTP request method and URL.
Create an instance of the FormEncoder
class to encode the form parameters and set it as the encoder for the Feign client.
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);
}
}
Asked: 2022-09-24 11:00:00 +0000
Seen: 11 times
Last updated: Jan 22 '23