feign对接类型form-data显示报错Could not write request: no suitable HttpMessageConverter found for request

发布时间 2023-05-26 10:56:52作者: Larson#HL

一、问题简介

在pringboot中利用feign对接第三方接口上传文件

@PostMapping(value = "/polarion/catl-workItem/uploadAtt", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public JSONObject uploadFile(@RequestPart(value = "file") MultipartFile file, @RequestParam("itemId") String itemId,
                                 @RequestParam("projectId") String projectId,@RequestParam("fileName") String fileName);

运行显示上传文件报错Could not write request: no suitable HttpMessageConverter found for request

 

解决办法:

1、pom.xml中添加依赖

<dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.0.3</version>
        </dependency>

        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.0.3</version>
        </dependency>

2、添加配置类

import feign.Logger;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

@Configuration
public class FeignMultipartSupportConfig {

    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder(){
        return  new SpringFormEncoder();
    }

    @Bean
    public Logger.Level multipartLoggerLevel(){
        return Logger.Level.FULL;
    }
}

3、再次调用显示调用成功

{
    "success": true,
    "message": "附件上传成功"
}