前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Feign经典错误

Feign经典错误

作者头像
zhaozhen
发布2022-04-27 19:06:30
9550
发布2022-04-27 19:06:30
举报
文章被收录于专栏:微瞰Java后端开发

Incompatible fallbackFactory instance. Fallback/fallbackFactory of type clas

失败原因:是我用的是fallbackFactory来进行回退,但是feignclient注解定义回退的类型是fallback,类型不一致。在调用报错时会校验fallabck或fallbackFactory是不是符合要求 正常fallback模式示例

代码语言:javascript
复制
@FeignClient(name = "mall-shop-e",
        url = "${feign.urls.mall-shop-e:}",
        fallback = MallOrderEClientFallback.class,
        configuration = FeignConfiguration.class)
public interface MallOrderEClient {

    @PostMapping("/rpc/external/channelPrice/get/list")
    public CommonResult<PageWrapper<List<ChannelPriceVo>>> queryChannelPriceList(@RequestBody ChannelPriceSearchReq req);
}



@Slf4j
@Component
public class MallOrderEClientFallback implements MallOrderEClient{
    @Override
    public CommonResult<PageWrapper<List<ChannelPriceVo>>> queryChannelPriceList(ChannelPriceSearchReq req) {
        return null;
    }
}

正常fallbackfactory示例。与fallback相比可以获取回滚的原因

代码语言:javascript
复制
@FeignClient(name = "finance-product",
        contextId = "MallPaymentClient",
        fallbackFactory = com.fosunhealth.orcas.service.feign.MallPaymentClientFallback.class,
        configuration = FeignConfiguration.class)
public interface MallPaymentClient {

    /**
     * 获取业务单元列表
     */
    @PostMapping("/rpc/costRelationManager/costRelationDetail")
    Result<CostRelationDetailResp> getBusinessUintList();

}


@Slf4j
@Component
public class MallPaymentClientFallback implements FallbackFactory<MallPaymentClient> {
    @Override
    public MallPaymentClient create(Throwable cause) {
        return new MallPaymentClient() {
            @Override
            public Result<CostRelationDetailResp> getBusinessUintList() {
                log.error("getBusinessUintList error ", cause);
                return null;
            }
        };
    }
}

上传文件使用Feign在不同服务之间传递

代码语言:javascript
复制
    /**
     * 导入
     * @param file 文件
     * @return
     */
    @PostMapping(value = "/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public CommonResult<ImportResp> imports(@RequestPart("file") MultipartFile file,@RequestParam String userName);

主要是consumes = MediaType.MULTIPART_FORM_DATA_VALUE 。不加上这个就无法在服务之间传递文件

feign导出文件如何传递

服务提供方

代码语言:javascript
复制
    /**
     * 导出报表
     * @param response
     */
    @PostMapping("export")
    public void export(HttpServletResponse response,BizProjectPriceSearchReq req){
        bizProjectPriceService.export(response,req);
    }

feignclient内

代码语言:javascript
复制
    /**
     * 导出报表
     */
    @PostMapping("export")
    public Response export(BizProjectPriceSearchReq req);

导出文件处理

代码语言:javascript
复制
  /**
     * 导出报表
     * @param response
     */
    public void export(HttpServletResponse response, BizProjectPriceSearchReq req){
        Response feignResponse = projectPriceClient.export(req);
        if (!CollectionUtils.isEmpty(feignResponse.headers().get("content-type"))) {
            response.setHeader("content-type", feignResponse.headers().get("content-type").stream().findFirst().get());
        }
        if (!CollectionUtils.isEmpty(feignResponse.headers().get("content-disposition"))) {
            response.setHeader("Content-disposition", feignResponse.headers().get("content-disposition").stream().findFirst().get());
        }
        response.setCharacterEncoding("utf-8");

        Response.Body body = feignResponse.body();
        InputStream fileInputStream = null;
        OutputStream outputStream = null;
        try {
            fileInputStream = body.asInputStream();
            outputStream = response.getOutputStream();
            byte[] bytes = new byte[1024];
            int len;
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
        } catch (Exception e) {
            log.warn("export throw exception e={}", e);
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                    outputStream.flush();
                }
            } catch (Exception e) {
                log.error("export close stream throw exception e={}", e);
            }
        }

    }

前端 如何请求

如何接收

代码语言:javascript
复制
export function downloadFile(res, type, fileName) {
  const blob = new Blob([res.data], {type: type||'application/octet-stream'});
  console.log('blob', blob);
  const downloadElement = document.createElement('a');
  const href = window.URL.createObjectURL(blob);
  const contentDisposition=res.headers['content-disposition'];
  let subIndex=contentDisposition.lastIndexOf('\'');
  if (subIndex==-1) {
    subIndex=contentDisposition.lastIndexOf('=');
  }
  downloadElement.download = fileName||decodeURIComponent(contentDisposition.substring(subIndex+1));
  downloadElement.href = href;
  document.body.appendChild(downloadElement);
  downloadElement.click();
  document.body.removeChild(downloadElement);
  window.URL.revokeObjectURL(href);
}

Body parameters cannot be used with form parameters

传递文件时,其他参数也需要使用@RequestParam

代码语言:javascript
复制
/**
* 导入
* @param file 文件
* @return
*/
@PostMapping(value = "/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public CommonResult<ImportResp> imports(@RequestPart("file") MultipartFile file,@RequestParam String userName);

请求方式为get但是提示为Request method 'POST' not supported"

代码语言:javascript
复制
"errMsg":"org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported"}

原因就是在声明时未使用@RequestParam

代码语言:javascript
复制
    public CommonResult<ChannelPriceGoodsInfoVO> channelPriceInfo( Long id);
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-04-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 微瞰技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Incompatible fallbackFactory instance. Fallback/fallbackFactory of type clas
  • 上传文件使用Feign在不同服务之间传递
  • feign导出文件如何传递
  • Body parameters cannot be used with form parameters
  • 请求方式为get但是提示为Request method 'POST' not supported"
相关产品与服务
腾讯云 BI
腾讯云 BI(Business Intelligence,BI)提供从数据源接入、数据建模到数据可视化分析全流程的BI能力,帮助经营者快速获取决策数据依据。系统采用敏捷自助式设计,使用者仅需通过简单拖拽即可完成原本复杂的报表开发过程,并支持报表的分享、推送等企业协作场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档