fegin 图片上传失败解决方法

报错 :

1 : No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer.

2 :  "message": "Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile] and content type [multipart/form-data]"

需要引入依赖:

    
         
            io.github.openfeign.form
            feign-form
            3.2.2
        
        
            io.github.openfeign.form
            feign-form-spring
            3.2.2
        
    

对外接口:


@Api(tags = "图片上传", description = "图片上传")
@RestController
@RequestMapping("/metadata/upload")
public class UploadController {


    @Autowired
    private UploadClient uploadClient;

    @ApiOperation(value = "图片上传",notes = "图片上传")
    @ApiResponses({
            @ApiResponse(code = 400, message = "数据错误"),
            @ApiResponse(code = 500, message = "内部错误"),
            @ApiResponse(code = 200, message = "成功", response = ApiResult.class)
    })
    @PostMapping
    public ApiResult upload(@ApiParam(name = "file",value = "上传的图片",required = true)@RequestPart(value = "file") MultipartFile file) {
        return uploadClient.upload(file);
    }

}

client:

#错误2,添加configuration配置
@FeignClient(name = "parts-business-metadata/metadata/upload",configuration =UploadClient.MultipartSupportConfig.class)
@Component
public interface UploadClient {


    class MultipartSupportConfig {

        @Autowired
        private ObjectFactory messageConverters;

        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }

    #报错1 ,添加consumers,produces 
    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE,produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    ApiResult upload(@RequestPart(value = "file") MultipartFile file);
}

参考:https://www.2cto.com/kf/201802/722369.html

你可能感兴趣的:(记录)