Using spring-mvc annotations, how can I define an @FeignClient that can POST form-url-encoded?
Full Java code with a simplified version of kazuar solution, works with Spring Boot:
import java.util.Map;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE;
@FeignClient(name = "srv", url = "http://s.com", configuration = Client.Configuration.class)
public interface Client {
@PostMapping(value = "/form", consumes = APPLICATION_FORM_URLENCODED_VALUE)
void login(@RequestBody Map form);
class Configuration {
@Bean
Encoder feignFormEncoder(ObjectFactory converters) {
return new SpringFormEncoder(new SpringEncoder(converters));
}
}
}
Dependency:
org.springframework.cloud
spring-cloud-starter-openfeign