Java IDEA feign调用上传文件MultipartFile以及实体对象亲测可行
- 1. 报错 java.lang.IllegalStateException: Body parameter cannot be used with form parameters
- 2. 解决
- 参考
1. 报错 java.lang.IllegalStateException: Body parameter cannot be used with form parameters
原因:PostMapping默认 application/json;charset=UTF-8
上传文件时需要 multipart/form-data
每个参数都需要 @RequestParam 或者 @RequestPart
2. 解决
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annatation.*;
import org.springframework.web.multipart.MultipartFile;
@FeignClient(name="testName")
@Component
public interface StatService {
// 仅上传实体内容 ok
@PostMapping(value = "/add")
ResponseDTO addObj(@RequestBody CompanyObject task);
// 上传实体内容+文件 ok
@PostMapping(value = "/addFileObj",produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResponseDTO addObj2(@RequestPart(value = "file",required = false) MultipartFile file, @RequestPart("task") String taskStr);
}
参考
- https://blog.csdn.net/Steriles_/article/details/129028126
- https://www.jianshu.com/p/f5081f054a34