springcloud 版本号 2.x
<version>2.0.9.RELEASE</version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
对应的openfeign版本号 :(注意使用3.0.8版本号或者其他的都会报错 解决方案很多都是吧http-core 升级到10版本, 根本不用, 换成下面的3.0.3版本即可)
<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>
服务提供者:
@PostMapping(value = "/buyer/upload/uploadImgFeign",produces = "application/json;charset=UTF-8",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Res<String> uploadImgFeign(@RequestParam("code") Long code, @RequestPart("file") MultipartFile file) {
System.out.println("code = " + code);
return null;
}
feign的写法
@FeignClient(value = "img-service", configuration = ImgClient.ClientConfiguration.class)
public interface BaseImgClient {
/**
* 判断平台是否支持商家功能
* //不需要登录consumes 必须
*/
@RequestMapping(value = "/upload,method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
RespData uploadImgFeign(@RequestParam("code") Long code, @RequestPart("file") MultipartFile file);
/**
* 配置类
*/
class ClientConfiguration {
/**
* 此处注入的是: ObjectFactory<HttpMessageConverters>
*/
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
}
服务调用者
@PostMapping(value = "/upload", produces = "application/json; charset=utf-8")
@ResponseBody
public Res<String> upload(@RequestParam("code") Long code, @RequestPart("file")MultipartFile file) {
imgClient.uploadImgFeign(code,file);
return null;
}
事实上: feign 和 服务调用者 的注解使用 requestBody也可以程功 唯独使用其他作者说的 使用requestParam不行.
===========================我是版本分割线啦拉============================================
springcloud 1.X 我搞了又搞, 改了又改, 实在不行啊啊啊啊啊啊啊
待续....
朋友最后用的是 httpclient 请求过去的 , 其实就是 自己本地A请求自己本地B, 1.0我也不知道怎么弄了, 工期实在是忙