Body parameters cannot be used with form parameters - Feign client with Headers and json data

Seetharamani Tmr picture Seetharamani Tmr · Apr 24, 2017 · Viewed 11.8k times · Source

I have a FeignClient like this

@RequestLine("POST /enroll")
@Headers({ "header1: {header1}", "header2: {header2}", "Content-Type: application/json" })
ResponseDto enroll(@Param("header1") String header1,@Param("header1") String header1, RequestDto requestDto)throws MyCustomException;

` I am not using spring cloud netflix. But I am keep getting the below exception.

Caused by: java.lang.IllegalStateException: Body parameters cannot be used with form parameters.
at feign.Util.checkState(Util.java:128)
at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:112)
at feign.Contract$BaseContract.parseAndValidatateMetadata(Contract.java:64)
at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:146)
at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:53)
at feign.Feign$Builder.target(Feign.java:209)
at feign.Feign$Builder.target(Feign.java:205)

I am instantiating my client like this.

return Feign.builder()
        .encoder(new JacksonEncoder())
        .decoder(new JacksonDecoder())
        .logger(new Slf4jLogger())
        .logLevel(Logger.Level.FULL)
        .target(RegularFeignClient.class, url);

Answer

Seetharamani Tmr picture Seetharamani Tmr · Apr 24, 2017

Wow this a tricky one. The order of parameters matter here.

@RequestLine("POST /enroll")
@Headers({ "header1: {header1}", "header2: {header2}", "Content-Type: application/json" })
ResponseDto enroll(RequestDto requestDto, @Param("header1") String header1,@Param("header1") String header1)throws MyCustomException;

This works!!!

Thanks to my senior developer. He found it.