[Java] feignclient如何支持第三方application/x-www-form-urlencoded的接口

第三方接口,只支持application/x-www-form-urlencoded的数据提交方式,解决过程比较曲折,记录一下

 

解决方案一(后面证实是失败的,仅供参考)

通过自定义FeignClient的配置类来解决
proxy代码参考
@ConditionalOnProperty("flan.api.url")
@FeignClient(name = "${flan.api.url:}",
        url = "${flan.api.url:}",
        path = "/OpenPlatform",
        configuration = {FeignClientConfig.class, FlanOrderProxy.Configuration.class}
)
public interface FlanOrderProxy {
 
 
    @PostMapping(value = "/pushOrderDetail", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    DataResult<Object> push(
            @RequestHeader Map<String, String> headerMap,
            @RequestBody Map<String, ?> postData
    );
 
    @GetMapping("/cancelOrder?order_no={orderNo}")
    DataResult<Object> cancel(
            @RequestHeader Map<String, String> headerMap,
            @PathVariable(value = "orderNo") String orderNo
    );
 
    class Configuration {
        @Bean
        @Primary
        Encoder feignFormEncoder(ObjectFactory<HttpMessageConverters> converters) {
            return new SpringFormEncoder(new SpringEncoder(converters));
        }
    }
}

解说

 

client代码参考
public DataResult<Object> push(StoreSaasFlanOrderPushRequest request) {
        Map<String, String> postData = new HashMap<>();
        postData.put("detail", JSON.toJSONString(request));
        return flanOrderProxy.push(
                FlanApiUtils.assembleHeaders(request.getAuthorization()),
                postData
        );
    }

解说

 

 

代码就差不多这样了,本地运行,正常!然后打包上测试环境,死了,老是报这个错!

Could not write request: no suitable HttpMessageConverter found for request type [java.util.HashMap] and content type [application/x-www-form-urlencoded]

这个错误的意思是,proxy类根本就没有使用到 Configuration这个类去encode数据!也就是说

 

 

调试了好久,一开始以为是test的代码分支有问题,把test分支checkout到本地,也是能正常运行的!没办法,只能又google了!

 

解决方案二

在不断搜索和尝试方案之后,终于找到了能解决的方式了!
参考文章: https://blog.csdn.net/rtuujnncc/article/details/91867685
proxy代码参考
@ConditionalOnProperty("flan.api.url")
@FeignClient(name = "${flan.api.url:}",
        url = "${flan.api.url:}",
        path = "/OpenPlatform",
        configuration = {FeignClientConfig.class}
)
public interface FlanOrderProxy {
 
    @PostMapping(value = "/pushOrderDetail", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    DataResult<Object> push(
            @RequestHeader Map<String, String> headerMap,
            @RequestBody MultiValueMap<String, ?> postData
    );
 
    @GetMapping("/cancelOrder?order_no={orderNo}")
    DataResult<Object> cancel(
            @RequestHeader Map<String, String> headerMap,
            @PathVariable(value = "orderNo") String orderNo
    );
 
}

解说

 

client代码参考

public DataResult<Object> push(StoreSaasFlanOrderPushRequest request) {
    MultiValueMap<String, String> postData = new LinkedMultiValueMap<>();
    postData.add("detail", JSON.toJSONString(request));
    return flanOrderProxy.push(
            FlanApiUtils.assembleHeaders(request.getAuthorization()),
            postData
    );
}

解说

 

 

本地运行,正常!测试环境运行,正常!Mission completed!


Have fun with Java & Spring!

 

 

 

 

 
 
posted @ 2021-11-04 17:52  DavidHHuan  阅读(2965)  评论(0编辑  收藏  举报