最近在项目中使用feign,但是使用POST请求传输对象却总是报错:
feignClient代码:
@PostMapping(FEIGN_PREFIX + "/add")
GeneralResult addDeviceInfo(@RequestBody DeviceInfoDTO deviceInfoDTO);
实现代码:
@Override
@PostMapping(FEIGN_PREFIX + "/add")
public GeneralResult addDeviceInfo(@RequestBody DeviceInfoDTO deviceInfoDTO) {
......
}
但是调用会报错 DeviceInfoDTO not a type supported by this encoder
检查了很多次找到原因:
1.当前项目POM文件引用 文件上传项目api
2.文件上传项目api由于要使用feign上传文件所以应该配置:
@FeignClient(
value = CommonConstant.APPLICATION_FASTDFS_NAME, configuration = FeignClient.MultipartSupportConfig.class
)
public interface FeignClient {
String FEIGN_PREFIX="/fastdfs";
/**
* 上传文件
* @param file
*/
@PostMapping(value=FEIGN_PREFIX + "/fastDfsUpload",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
GeneralResult<FastFileDTO> fastDfsUpload(@RequestPart("file") MultipartFile file,@RequestParam("type")int type) throws IOException;
class MultipartSupportConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
}
}
但是我却将MultipartSupportConfig单独写到配置文件中并且在其上添加了@Configuration注解成为了全局配置,以至于无法正常传输实体参数。
参考链接:参考链接
本文探讨了在使用Feign进行POST请求时遇到的实体参数传输错误:DeviceInfoDTO notatypesupportedbythisencoder。问题源于MultipartSupportConfig的不当配置,将其作为全局配置导致实体参数无法正常传输。通过正确配置FeignClient,可以解决此问题。
1519





