spring cloud feign 上传文件 not a type supported by this encoder

本文介绍如何在SpringCloud项目中使用Feign进行文件上传的配置,包括依赖导入、配置类编写及接口定义,同时解决仅能上传文件的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中使用了Spring cloud 中的feign调用了另一个系统的接口。

当传输文件时需做另外配置

导入pom

<dependency>  
	<groupId>io.github.openfeign.form</groupId>  
	<artifactId>feign-form-spring</artifactId>  
	<version>3.2.2</version>  
</dependency>  
<dependency>  
	<groupId>io.github.openfeign.form</groupId>  
	<artifactId>feign-form</artifactId>  
	<version>3.2.2</version>  
</dependency>

写一个配置类

import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/** 
* @Description: Spring cloud
* @params 
* @return 
* @date 2019/2/20 12:55 PM
*/
@Configuration
public class FeignSupportConfig {
  @Bean
  public Encoder feignFormEncoder() {
    return new SpringFormEncoder();
  }
}

使用配置类


configuration = 配置类

consumes = MediaType.MULTIPART_FORM_DATA_VALUE 必须加
@RequestPart("file")    上传文件必须使用
@FeignClient(name = "aiface-producer",path="/facecmp",configuration = FeignSupportConfig.class)
@Component
public interface FaceCmpFeign {


	@PostMapping(value="/searchByInfo",
			consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public ReturnResult searchByInfo(
			@RequestPart("file") MultipartFile file,
			@RequestParam("videoIds") String videoIds,
			@RequestParam("faceSetId") String faceSetId,
			@RequestParam("info") String info,
			@RequestParam(value = "pageNo",required=false) Integer pageNo,
			@RequestParam(value = "pageSize",required=false) Integer pageSize) throws Exception;
}

配置完毕可以上传文件了,但是,这样配置后,只能上传文件了,如果你不上传文件就会报错。其他当接口也调用不了了。

配置类修改一下


import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
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;


/** 
* @Description: Spring cloud 文件上传配置类 此类只能添加单文件,并且不支持参数传输对象
* @params 
* @return 
* @date 2019/2/20 12:55 PM
*/
@Configuration
public class FeignSupportConfig {
  @Autowired
  private ObjectFactory<HttpMessageConverters> messageConverters;

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

 

 

TCP 主动模式和被动模式的区别

### Feign 客户端不支持 POST 请求方法的解决方案 Feign 默认配置下确实存在一些限制,特别是在处理 `POST` 方法时。为了使 Feign 支持 `POST` 请求并传递数据体,可以采取以下几种方式: #### 使用 @RequestBody 和 @PostMapping 注解 当定义 Feign 接口时,应确保使用了合适的注解来指定 HTTP 方法及其参数类型。对于 `POST` 请求,通常会结合 `@PostMapping` 或者 `@RequestMapping(method = RequestMethod.POST)` 来声明请求的方式。 ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @FeignClient(name="exampleService", url="http://localhost:8080") public interface ExampleClient { @PostMapping(value="/api/resource", consumes="application/json") String createResource(@RequestBody MyDataObject data); } ``` 这里指定了消费的内容类型为 JSON,并通过 `@RequestBody` 将 Java 对象序列化到请求主体中[^1]。 #### 配置 Encoder 解决方案 如果遇到更复杂的情况或是默认编码器无法满足需求,则可以通过自定义编码器来进行扩展。Spring Cloud 提供了一个叫做 `spring-cloud-starter-openfeign` 的依赖库,它允许开发者轻松集成 Jackson 序列化工具作为默认的消息转换机制之一。另外还可以实现自己的 `Encoder` 实现类来自定义如何将对象写入HTTP消息体内。 ```yaml feign: httpclient: enabled: true encoder: com.example.CustomJacksonEncoder # 自定义编码器路径 ``` 同时也可以利用 Spring MVC 已有的功能,比如设置全局的 HttpMessageConverters 或是在特定场景下调用 RestTemplate 进行辅助操作。 #### 处理 Content-Type 不匹配问题 有时即使设置了正确的注解仍然会出现错误提示说服务器拒绝接受请求,这可能是由于发送方和服务端之间关于媒体类型的协商出现了分歧所引起的。此时应当确认双方都同意使用的 MIME 类型(如 application/json),并且保证客户端能够正确地解析响应结果。 ```java // 确认服务端接口已正确定义接收JSON格式的数据 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<MyResponse> handlePostRequest(@Valid @RequestBody MyRequest request){ ... } // 在 feign client 中同样要注明期望返回的结果形式 @RequestMapping(method = RequestMethod.POST, value = "/path", headers = "Content-Type=application/json", consumes = "application/json", produces = "application/json") MyResponse doPostCall(MyRequest body); ``` 以上措施有助于解决 Feign 客户端在执行 `POST` 调用时不被支持的问题[^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SUNbrightness

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值