使用Feign调用form-data的接口

当不想用restTemplate处理form-data接口时,采取以下方式: 1.设计一个转换器

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

2.在Feign类上,加入转换器配置

@FeignClient(value = "Student",configuration = {
          
   FeignRequestIntercepter.class})
public interface StudentFeignClient {
          
   ...}

3.在Feign定义得方法中,定义接口

@PostMapping("/innerScore/viewScoreRemark")
Response viewScoreRemark(JSONObject json);

如果是多参数,则添加@RequestParam注解声明变量

@PostMapping("/innerScore/viewScoreRemark")
Response viewScoreRemark(@RequestParam Long classId, @RequestParam Long studentId);

否则报错 Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.iqiyi.nexus.dto.Response xxx.viewScoreRemark(java.lang.Long,java.lang.Long)

经验分享 程序员 微信小程序 职场和发展