Running @FeignClient with application/x-www-form-urlencoded throws IllegalStateException: Method has too many Body parameters
See original GitHub issueIs it possible to use feign-form’s support for application/x-www-form-urlencoded
forms with Spring Cloud’s @EnableFeignClients
/@FeignClient
setup?
Currently, I am getting the following exception when starting the ApplicationContext
:
java.lang.IllegalStateException: Method has too many Body parameters: public abstract java.lang.String com.example.feigntest.MyClient.foo(java.lang.String,java.lang.String,java.lang.String)
To me, that is an indication that loading the feign-form
support didn’t quite work.
I’m using org.springframework.cloud:spring-cloud-starter-openfeign
, org.springframework.boot:spring-boot-starter
and for dependencyManagement
org.springframework.cloud:spring-cloud-dependencies:Greenwich.SR3
, all with a org.springframework.boot:spring-boot-starter-parent:2.1.9.RELEASE
parent.
My setup, running against a simple standalone wiremock with --print-all-network-traffic
flag and a single simple mock:
@EnableFeignClients
@SpringBootApplication
public class FeignTestApplication implements CommandLineRunner {
@Autowired private MyApp myApp;
public static void main(String[] args) { SpringApplication.run(FeignTestApplication.class, args); }
@Override public void run(String... args) { myApp.run(); }
}
@Component
public class MyApp {
@Autowired private MyClient myClient;
public void run() { String result = myClient.foo("a", "b", "c"); }
}
@FeignClient(name="myClient", url = "http://localhost:8080", configuration = MyClientConfig.class)
public interface MyClient {
@RequestMapping(method = RequestMethod.POST, value = "/foo")
@Headers("Content-Type: application/x-www-form-urlencoded")
public String foo(@Param("p1") String p1, @Param("p2") String p2, @Param("p3") String p3);
}
public class MyClientConfig { // NB: is @Configuration required here?
@Autowired private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean public Encoder feignFormEncoder () { return new SpringFormEncoder(new SpringEncoder(messageConverters)); }
}
When I remove the Spring Cloud feign annotations (@EnableFeignClients
, @FeignClient
) and replace both the autowired myClient
property in MyApp
with private MyClient myClient = Feign.builder().encoder(new FormEncoder()).target(MyClient.class, "http://localhost:8080");
and the @RequestMapping
annotation in MyClient
with @RequestLine("POST /foo")
, it all works.
So, I know I haven’t gone completely wrong. I’m basically just wondering if feign-forms
support for application/x-www-form-urlencoded
forms can be made to work with Spring Cloud’s @EnableFeignClients
/@FeignClient
magic (and I’ve made a mistake somewhere), or if the exception is to be expected?
Also (and probably somewhat unrelated), would I have to use the @Configuration
annotation on the MyClientConfig
class if I used the @FeignClient
annotation and wanted to refine the config programatically?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:7
NB: Looks like the following does the trick, without the
@Headers
annotation:with a wrapper whose properties match the params from before:
Log:
Oh, and btw: the
@Configuration
is not required.I tried to use following code, but I got the same error when I was using LinkedHasMap.
I tried to use FormEncoder Configuration, but when I try to do a normal request, in other Client he tries to encode my request without set configuration in Client properties.
Headers class:
Produces class:
If someone knows any solution without use Encoder bean I’ll be thankfull