2017-02-23 75 views
3

这是我的假死接口如何通过春季云假死发送POST请求

@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:应用/ JSON;字符集= 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请求,我想我应该与编码器更多的东西。 感谢您的帮助。所有的

回答

1

首先你shold改变假死接口这样

@FeignClient(
       configuration = FeignSimpleEncoderConfig.class 
) 
public interface MpiClient { 

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

那么你应该在假死的配置设置编码器

public class FeignSimpleEncoderConfig { 

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

在我看来,那地图无效形式的身体。 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)); 
    } 
} 

摇篮依赖关系:

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参数调用它。