feign上传文件--产生的错误

发现文件传不上去,a服务就报错com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile[“inputStream”]->java.io.FileInputStream[“fd”])

项目为spring cloud 项目,项目中对各部分能力业务进行了拆分,将公共的服务能力放在一个模块当中,通过

Feign 的方式 进行调用,feign 调用的本质还是http内部通信请求,实现模块之间的调用。
消费端代码

@ApiOperation("IEMS内部货号与企业物料转换-导出/入内部货号与成品码关系Excel.-导入")
    @PostMapping("/doImportInInformationApi")
    JsonResult doImportInInformationApi( @RequestParam(value = "file") MultipartFile file,@RequestParam(value = "usid")@ApiParam("登录用户") String usid,@RequestParam(value = "comp") String comp) throws SQLException, ParseException ;

通过feign进行文件上传时,feign 侧示例代码如下:

@FeignClient("developmentora8domainservice")
public interface DevelopOra8FeignClient {
@ApiOperation("IEMS内部货号与企业物料转换-导出/入内部货号与成品码关系Excel.-导入")
    //@PostMapping("/dealTznoInfor/doImportInInformationApi")
    @RequestMapping(value = "/dealTznoInfor/doImportInInformationApi", consumes = MediaType.MULTIPART_FORM_DATA_VALUE,method = {RequestMethod.POST},produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},headers = "content-type=multipart/form-data")
    JsonResult doImportInInformationApi(@RequestPart("file") MultipartFile file, @RequestParam(value = "usid") String usid, @RequestParam(value = "comp") String comp) throws SQLException, ParseException ;
}

文件属性需要使用 @RequestPart 属性,而且在请求中添加 consumes = MediaType.MULTIPART_FORM_DATA_VALUE ,

文件表单上传请求通常使用的ContentType为 multipart/form-data ,通过以上直接调用feign的方式即可实现feign 文件上传

以下为 @RequestPart 与 @RequestParam 的区别:

补充说明:
produces:它的作用是指定返回值类型,不但可以设置返回值类型还可以设定返回值的字符编码;

consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;

客户端代码

@ApiOperation("IEMS内部货号与企业物料转换-导出/入内部货号与成品码关系Excel.-导入")
   // @PostMapping(value = "/doImportInInformationApi",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @RequestMapping(value = "/doImportInInformationApi", consumes = "multipart/*",method = {RequestMethod.POST},headers="content-type=multipart/form-data")
    public JsonResult doImportInInformationApi(HttpServletRequest request, HttpServletResponse response,@RequestPart(value = "file") MultipartFile file,@RequestParam(value = "usid") String usid,@RequestParam(value = "comp") String comp) throws SQLException, ParseException, IOException {
        JsonResult saveShipingInformationApi = tznoDomainService.doImportInInformationApi(request,response,file,usid,comp);
        return saveShipingInformationApi;
    }

还遇到一个上传文件的接口,报错HttpMediaTypeNotAcceptableException Could not find acceptable representation

是因为单文件上传的时候,要用@RequestPart注解

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值