记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<ZMSpuInfoDTO> 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<T> 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<String, Object> request);}

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

3. 断点调试

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

如上图所示,最终会由 

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

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/178993.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ubuntu 20通过docker安装onlyoffice,并配置https访问

目录 一、安装docker &#xff08;一&#xff09;更新包列表和安装依赖项 &#xff08;二&#xff09;添加Docker的官方GPG密钥 &#xff08;三&#xff09;添加Docker存储库 &#xff08;四&#xff09;安装Docker &#xff08;五&#xff09;启动Docker服务并设置它随系…

电视剧剪辑,微课制作神器Camtasia的干货介绍,建议收藏。

最近无论是b站&#xff0c;抖音&#xff0c;快手等视频软件中都有不少微课视频、电视剧解说横空出世&#xff0c;通过这些“热度”带来的收益也是无法估量的&#xff0c;很多自媒体博主月入上万惹人羡慕。 不少朋友也想在这股短视频洪流中分一碗羹&#xff0c;但又苦于技术跟不…

【C++ std::max_element std::min_element std::minmax_element】

一 、std::max_element 寻找范围 [first, last) 中的最大元素。 (1) 用 operator< 比较元素。 (3) 用给定的二元比较函数 comp 比较元素。 (2),(4) 同 (1,3) &#xff0c;但按照 policy 执行。这些重载仅若 std::is_execution_policy_v<std::decay_t > (C20 前)std:…

Qt QWebSocket实现JS调用C++

目录 前言1、QWebChannel如何与网页通信2、QWebSocketQWebChannel与网页通信2.1 WebSocketTransport2.2 WebSocketClientWrapper2.3 初始化WebSocket服务器2.4 前端网页代码修改 总结 前言 本篇主要介绍实现JS调用C的另一种方式&#xff0c;即QWebSocketQWebChannel。与之前的…

JTS: 24 MinimumDiameter 最小矩形

文章目录 版本代码 版本 org.locationtech.jts:jts-core:1.19.0 链接: github 代码 package pers.stu.algorithm;import org.locationtech.jts.algorithm.MinimumDiameter; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Geometry; import…

基于stm32f103系列的简单软件I2C和硬件I2C通讯

这篇文章主要分为三个部分来阐述&#xff0c;分别是I2C的基本知识&#xff0c;软件I2C通讯&#xff0c;硬件I2C通讯。I2C的基本知识这一块&#xff0c;部分讲解以及图表来自b站江科大的up&#xff0c;很感谢这位up&#xff0c;大家可以关注一波。操作实现的时候&#xff0c;up使…

自己动手写编译器:实现编译器工具链中的词法解析工具 Flex

我们在上一节以手动的方式实现了一个词法解析器的 c 语言源码。它主要包含若干部分&#xff0c;第一部分就是输入缓存系统&#xff0c;用于从磁盘文件或者控制台上获取要解析的字符串。第二部分是数据读入逻辑&#xff0c;它主要通过调用输入系统的接口获得要解析的字符串&…

1 Supervised Machine Learning Regression and Classification

文章目录 Week1OverViewSupervised LearningUnsupervised LearningLinear Regression ModelCost functionGradient Descent Week2Muliple FeatureVectorizationGradient Descent for Multiple RegressionFeature ScalingGradient DescentFeature EngineeringPolynomial Regress…

企业动产抵押代表着什么?

动产抵押 动产抵押是指债务人或第三人以动产不移转占有而供担保的抵押形式。在债务人不履行债务时&#xff0c;抵押权人占有抵押动产&#xff0c;并就其出卖价金优先于其他债权而受清偿。由于不移转动产的占有而与以动产为标的的质权相区别&#xff0c;因动产抵押为不占有债务…

python socket编程2 - socket创建发送方所需参数的获得

使用socket进行进程间通信或者跨网络的计算机间通讯&#xff0c;有点类似日常生活中的发送快递。 根据发送方的需要&#xff0c;选择不同的物流公司&#xff1a; 在选择适合的公司和运输方式后&#xff0c;需要在app上做出选择&#xff0c;并根据要求填写一些信息。app会根据…

YOLO目标检测——树叶检测数据集下载分享【含对应voc、coco和yolo三种格式标签】

实际项目应用&#xff1a;生物多样性研究、林业管理、环境监测和教育科研等方面数据集说明&#xff1a;树叶分类检测数据&#xff0c;真实场景的高质量图片数据&#xff0c;数据场景丰富&#xff0c;总共十个类别。标签说明&#xff1a;使用lableimg标注软件标注&#xff0c;标…

【echarts】实现单线与多线滚轮联动、隐藏拖拽、关闭动画

单线滚轮联动 <!DOCTYPE html> <html> <head><meta charset"utf-8"><title>ECharts DataZoom</title><script src"https://cdn.jsdelivr.net/npm/echarts5.2.0/dist/echarts.min.js"></script> </hea…