发现文件传不上去,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注解