记feign调用第三方接口时header是multipart/form-data

1.请求第三方接口,用feign请求

请求第三方接口,用feign请求,header不通,feign的写法不同

调用时报错Could not write request: no suitable HttpMessageConverter found for request type [com.ccreate.cnpc.mall.dto.zm.ZMPageQueryDTO] and content type [multipart/form-data;charset=UTF-8]

1. 对方接口是multipart/form-data接收格式

package com.mall.controller.feign;

import com.mall.controller.feign.fallback.ZMGoodFeignFallback;
import com.mall.dto.zm.ZMPageQueryDTO;
import com.mall.dto.zm.ZMSpuInfoDTO;
import com.ccreate.common.response.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 商品
 */
@FeignClient(url = "${zm.host}", name = "cnpc-ZMGood", fallbackFactory = ZMGoodFeignFallback.class)
public interface ZMGoodFeign {

    /**
     * 商品列表查询
     *
     */
    @PostMapping(value = "/api/agent/goodsList", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    R getSpuList(@RequestBody ZMPageQueryDTO page);

}

对方返回的格式

  {
    code:200,
    message:"ok",
    data:{
        list:[
            {
                id:"10001661",
                name:"migo保温壶家用保温水壶大容量热水瓶304不锈钢热水壶保温瓶暖",
                sellingPoint:"304不锈钢 坚韧把手 24小时长效保温",
                skuType:"2",
                brandId:"100094",
                brandName:"miGo",
                categoryId:"100278",
                categoryIds:"100235,100273,100278",
                coverImage:"http://zmzx-b2b.oss-cn-hangzhou.aliyuncs.com/goods/2020052717090891958.jpg",
                marketPrice:"189.00",
                photos:[
                    "http://zmzx-b2b.oss-cn-hangzhou.aliyuncs.com/goods/2020052717090891958.jpg",
                    "http://zmzx-b2b.oss-cn-hangzhou.aliyuncs.com/goods/2020052717091363771.jpg",
                ],
                detailImages:[
                    "http://zmzx-b2b.oss-cn-hangzhou.aliyuncs.com/goods/2020052717110984525.jpg",
                    "http://zmzx-b2b.oss-cn-hangzhou.aliyuncs.com/goods/2020052717111337957.jpg",
                ],
                detailContent:"",
                deliverTime:"TIME24",
                servicePromise:[
                    "FREE_DELIVERY",
                    "PROMISE10"
                ],
                afterSaleRule:"",
                auditStatus : "1",
                agentRejectReason : "",
                skuList:[
                    {
                        "skuId":"80005071",
                        "code":"S20003VH.02",
                        "marketPrice":"199.00",
                        "agentPrice":"200.00"
                        "weight":"0.90",
                        "specList":[
                            {
                                "name":"容量",
                                "value":"2L",
                                "image":"http://zmzx-b2b.oss-cn-hangzhou.aliyuncs.com/goods/2020052717160413832.jpg"
                            },
                            {
                                "name":"颜色",
                                "value":"馥芮白",
                                "image":""
                            }
                        ],
                    },
                    {
                        "skuId":"80005072",
                        "code":"S20003VH.01",
                        "marketPrice":"199.00",
                        "agentPrice":"200.00"
                        "weight":"0.90",
                        "specList":[
                            {
                                "name":"容量",
                                "value":"2L",
                                "image":"http://zmzx-b2b.oss-cn-hangzhou.aliyuncs.com/goods/2020052717161011241.jpg"
                            },
                            {
                                "name":"颜色",
                                "value":"新草绿",
                                "image":""
                            }
                        ],
                    },
                    {
                        "skuId":"80005073",
                        "code":"S1506VH.02",
                        "marketPrice":"189.00",
                        "weight":"0.80",
                        "agentPrice":"200.00"
                        "specList":[
                            {
                                "name":"容量",
                                "value":"1.5L",
                                "image":"http://zmzx-b2b.oss-cn-hangzhou.aliyuncs.com/goods/2020052717162165903.jpg"
                            },
                            {
                                "name":"颜色",
                                "value":"馥芮白",
                                "image":""
                            }
                        ],
                    },
                    {
                        "skuId":"80005074",
                        "code":"S1506VH.01",

                        "weight":"0.80",
                        "marketPrice":"189.00",
                        "agentPrice":"200.00"
                        "specList":[
                            {
                                "name":"容量",
                                "value":"1.5L",
                                "image":"http://zmzx-b2b.oss-cn-hangzhou.aliyuncs.com/goods/2020052717162532347.jpg"
                            },
                            {
                                "name":"颜色",
                                "value":"新草绿",
                                "image":""
                            }
                        ],
                    }
                ],
                status:"1",
                agentStatus:"1",
                agentRejectReason:""
            },
        ],
        "total":1
    }
}

我们接收R对象

public class R implements Serializable {
    private static final int SUCCESS = 200;
    private static final String SUCCESS_MESSAGE = "success";
    private static final long serialVersionUID = 1L;
    private int code = 200;
    private String message = "success";
    private T data;
}

2. 对方接收格式是x-www-form-urlencoded

我们对应的feign请求是

package com.mall.controller.feign;

import com.mall.controller.feign.fallback.ThirdPriceFeignFallback;
import com.mall.dto.GoodSkuThirdPriceAddDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 三方商品价格
 */
@FeignClient(url = "${li.host}", name = "cnpc-price", fallbackFactory = ThirdPriceFeignFallback.class)
public interface ThirdPriceFeign {

//    @PostMapping(value = "/price", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @PostMapping(value = "/price",headers = {"content-type=application/x-www-form-urlencoded"})
    GoodSkuThirdPriceAddDTO getThirdPrice(@RequestBody MultiValueMap request);

}

这里参数必须是MultiValueMap,否则转换参数的时候就会报错

3. 断点调试

所有的参数封装都会走到org.springframework.cloud.openfeign.support.SpringEncoder#encode这个方法

记feign调用第三方接口时header是multipart/form-data_第1张图片

如上图所示,最终会由 

public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConverter 这个类解析参数

记feign调用第三方接口时header是multipart/form-data_第2张图片

最终组装好header和body参数,再去请求

4. 以后还有请求对方接口header带token或其他header信息的,再总结。 

你可能感兴趣的:(html,java,spring,cloud)