feign get请求
feign 请求参数默认是@RequestBody
如果是get请求要使用注解@RequestParam
Spring cloud feign 上传 multipartFile 文件
服务端
@PostMapping()
@ApiOperation(value = "文件上传", httpMethod = "POST", notes = "文件上传")
fun uploadPlist( @RequestParam("file") uploadFile: MultipartFile): ResponseEntity<*> {
try {
return uploadService.upload(uploadFile).let { ResponseEntity.ok(it) }
} catch (e: IOException) {
logger.error(e.toString()
}
}
feign 消费:
添加依赖:
compile 'io.github.openfeign.form:feign-form:3.0.3'
compile 'io.github.openfeign.form:feign-form-spring:3.0.3'
@FeignClient(ClientConstant.FILE, configuration = [FileClient.MultipartSupportConfig::class])
interface FileClient {
@PostMapping(value = ["/file"], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
fun upload(@RequestPart("file") uploadFile: MultipartFile): FileClientParam
//@Configuration //加上@Configuration注解表示全局都接受这个配置
class MultipartSupportConfig {
@Bean
fun multipartFormEncoder(): Encoder {
return SpringFormEncoder()
}
@Bean
fun multipartLoggerLevel(): feign.Logger.Level {
return feign.Logger.Level.HEADERS
}
}
}
注意:
1.@FeignClient(ClientConstant.FILE, configuration = [FileClient.MultipartSupportConfig::class])
在FeignClient 注解中单独添加配置类仅作用于当前类,在配置类上加@configuration 会作用于整个项目,
- @RequestPart("file") uploadFile: MultipartFile
请求参数目前必须用 file做key,否则可能接收不到参数,直接报400
feign调用坑很多,请谨慎使用....
file 转 multipartFile,使用multipartFile的实现类CommonsMultipartFile
依赖:
compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.2'
val xmlFile=File("/abc.txt)
val fileItem = DiskFileItemFactory().createItem("file",
MediaType.TEXT_PLAIN_VALUE, true, xmlFile.name) as DiskFileItem
try {
FileInputStream(xmlFile).use { input -> fileItem.outputStream.use { os -> IOUtils.copy(input, os) } }
} catch (e: Exception) {
throw IllegalArgumentException("Invalid file: $e", e)
}
val multi = CommonsMultipartFile(fileItem)
项目地址:https://github.com/athc/angle-cloud/tree/master/angle-business/business-feign
参考链接:http://blog.didispace.com/spring-cloud-starter-dalston-2-4/