How to send a POST request Spring Cloud Feign

This is my Feign interface.

@FeignClient(
        name="mpi",
        url="${mpi.url}",
        configuration = FeignSimpleEncoderConfig.class
)
public interface MpiClient {

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<String> getPAReq(@QueryMap Map<String, String> queryMap
    );
}

and my user configuration

public class FeignSimpleEncoderConfig {
    public static final int FIVE_SECONDS = 5000;

    @Bean
    public Logger.Level feignLogger() {
        return Logger.Level.FULL;
    }

    @Bean
    public Request.Options options() {
        return new Request.Options(FIVE_SECONDS, FIVE_SECONDS);
    }

    @Bean 
    @Scope("prototype")
    public Feign.Builder feignBuilder() {
        return Feign.builder()
                .encoder(new FormEncoder());
    }
}

If I send a request like this, I see that my request sends Content-Type: application / json; charset = UTF-8. But if I set the content type

consumes = "application/x-www-form-urlencoded"

I have this error message

feign.codec.EncodeException: Could not write request: no suitable HttpMessageConverter found for request type [java.util.HashMap] and content type [application/x-www-form-urlencoded]
    at org.springframework.cloud.netflix.feign.support.SpringEncoder.encode(SpringEncoder.java:108) ~[spring-cloud-netflix-core-1.1.7.RELEASE.jar:1.1.7.RELEASE]

How to send a POST request, I think I should do something else with Encoder. Thank you for your help.

+5
source share
3 answers

First of all, you should change your Feign interface as follows:

@FeignClient (
    configuration = FeignSimpleEncoderConfig.class
)
public interface MpiClient {
   @RequestMapping(method = RequestMethod.POST)
   ResponseEntity<String> getPAReq(Map<String, ?> queryMap);
}

Then you must install the encoder during the simulation of the configuration:

public class FeignSimpleEncoderConfig {
    @Bean
    public Encoder encoder() {
        return new FormEncoder();
    }
}
+3
source

, . MultiValueMap .

:

@FeignClient(name = "name", url="url", configuration = FromUrlEncodedClientConfiguration.class)
public interface PayPalFeignClient {

    @RequestMapping(value = "/", method = POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @Headers("Content-Type: application/x-www-form-urlencoded")
    String foo(MultiValueMap<String, ?> formParams);
}

Config:

@Configuration
public class FromUrlEncodedClientConfiguration {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    @Primary
    @Scope(SCOPE_PROTOTYPE)
    Encoder feignFormEncoder() {
        return new FormEncoder(new SpringEncoder(this.messageConverters));
    }
}

Gradle :

compile group: 'io.github.openfeign.form', name: 'feign-form', version: '2.0.2'
compile group: 'io.github.openfeign.form', name: 'feign-form-spring', version: '2.0.5'

, , MultivalueMap.

+3

json/xml/formhttpurl encoded

@Bean
public Encoder feignEncoder() {
    ObjectFactory<HttpMessageConverters> objectFactory = () ->
            new HttpMessageConverters(new FormHttpMessageConverter());
    return new SpringEncoder(objectFactory);
}

HttpMessageConverter MultiValueMap

0

Source: https://habr.com/ru/post/1670680/


All Articles