【问题标题】:How to send POST request by Spring cloud FeignSpring Cloud Feign如何发送POST请求
【发布时间】:2017-07-14 15:16:07
【问题描述】:

这是我的 Feign 界面

@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
    );
}

还有我的自定义配置

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());
    }
}

如果我发送这样的请求,我会看到我的请求发送 Content-Type: application/json;charset=UTF-8。 但是如果我设置内容类型

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

我有这个错误信息

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]

如何发送 POST 请求,我想我应该用 Encoder 做更多的事情。 感谢您的帮助。

【问题讨论】:

    标签: spring-cloud-netflix netflix-feign spring-cloud-feign


    【解决方案1】:

    首先你应该像这样改变你的 Feign 界面:

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

    那么你应该在feign配置时设置编码器:

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

    【讨论】:

    • 我正在尝试在我的项目中使用 Encoder bean(spring boot 2.0.2.RELEASE 和 feign-form:3.4.1 & feign-form-spring:3.4.1 )但我有FormEncoder 类的问题。问题是我无法导入 feign.form.FormEncoder。有什么建议吗?谢谢
    • 我不知道。我用过spring-boot 1.3.8
    【解决方案2】:

    在我看来 Map 对表单体无效。 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);
    }
    

    配置:

    @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 编码

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

      重要的FormHttpMessageConverter只序列化MultiValueMap子类

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-11
        • 2023-04-06
        • 2016-05-29
        • 2012-07-04
        • 2017-01-13
        • 2018-11-02
        • 2016-05-17
        • 1970-01-01
        相关资源
        最近更新 更多