feign 客户端 同时支持文件上传和实体类作为参数接收

想让feign 客户端,这样既支持文件上传也支持实体类作为参数接收,  并放一个实体RequestBody 中.

一直报错:

2020-03-25 19:02:02.388 ERROR 3217 --- [ZXr4fAmWFX][io-22223-exec-1] core.ErrorHandler        : system occur error: 

feign.codec.EncodeException: Could not write request: no suitable HttpMessageConverter found for request type [model.dto.feign.DTO] and content type [multipart/form-data]
	at org.springframework.cloud.openfeign.support.SpringEncoder.encode(SpringEncoder.java:143)
	at feign.ReflectiveFeign$BuildEncodedTemplateFromArgs.resolve(ReflectiveFeign.java:372)
	at feign.ReflectiveFeign$BuildTemplateByResolvingArgs.create(ReflectiveFeign.java:224)
	at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:74)
	at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)
	at com.sun.proxy.$Proxy208.uploadAvatar(Unknown Source)

可以通过以下配置实现上述需求

1. 配置类


import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;

/**
 * 让 feign支持含MultiPart的dto 解析
 *
 * Created on 2020-03-25
 */
//@Configuration
public class MultiPartSupportConfiguration {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    public Encoder feignFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }
}

2. feign 客户端

@FeignClient(value = "XXX", url = "https://xxx.com", configuration =
        MultiPartSupportConfiguration.class)
public interface ClientDev  {
   @PostMapping(value = "/api/v1/users.setAvatar",consumes = MediaType.MULTIPART_FORM_DATA_VALUE) Result uploadAvatar(@RequestBody DTO dto, @RequestHeader("X-Auth-Token") String token, @RequestHeader("X-User-Id") String uid);
}

 

3.DTO类

@Data
public class DTO {

    private String username;
    private MultipartFile image;

}

这样既支持文件上传也支持实体类作为参数接收

4.参考:

https://blog.csdn.net/gududedabai/article/details/79895893

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Feign客户端是一个轻量级的HTTP客户端,它使用注解方式定义API接口,并且能够与Spring Cloud等微服务框架无缝集成。下面是实现Feign客户端的步骤: 1. 添加依赖 在Maven或Gradle中添加Feign客户端的依赖。 Maven: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` Gradle: ```groovy implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' ``` 2. 定义API接口 使用Feign注解定义调用的API接口,例如: ```java @FeignClient(name = "example-service") public interface ExampleServiceClient { @GetMapping("/example") String getExample(); @PostMapping("/example") void postExample(@RequestBody Example example); } ``` 3. 注入Feign客户端 在需要调用API的代码中注入Feign客户端,例如: ```java @Service public class ExampleService { private final ExampleServiceClient exampleServiceClient; public ExampleService(ExampleServiceClient exampleServiceClient) { this.exampleServiceClient = exampleServiceClient; } public String getExample() { return exampleServiceClient.getExample(); } public void postExample(Example example) { exampleServiceClient.postExample(example); } } ``` 4. 配置Feign客户端 可以在配置文件中配置Feign客户端的一些属性,例如: ```yaml example-service: url: http://example-service connectTimeout: 5000 readTimeout: 5000 ``` 以上就是实现Feign客户端的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值