SpringBoot微服务之间文件(MutipartFile)传输(Eureka,Feign)踩的坑:ClassNotDfException: feign.Request$Body

在实现SpringBoot微服务间使用MutipartFile传输时遇到ClassNotDfException,错误源于feign.Request$Body。解决方案是仅引入特定jar包,避免与其他包冲突。遇到相同问题的开发者可以尝试文中建议。

今天第一次遇到需要微服务之间传输MutipartFile的业务场景,网上找了一圈发现最终报错:ClassNotDfException: feign.Request$Body解决不了。

具体这样的,网上都差不多:

@Configuration
public class MultipartSupportConfig {
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    public Encoder feignFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }
}
@FeignClient(name = "xxx", configuration = MultipartSupportConfig.class)
@Component
public interface CoreEurekaClient {

    @PostMapping(value = "/core/file/upload/{serverId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public void uploadFile(@RequestPart MultipartFile file,
                           @PathVariable Integer serverId);

}
@PostMapping("/upload/{serverId}")
@ApiOperation("文件上传接口")
public void uploadFile(@RequestParam MultipartFile file,
                       @PathVariable Integer serverId){

    fileService.uploadFile(file, serverId);
}
### Feign 404 错误的原因分析与解决方案 Feign 是一种声明式的 Web Service 客户端,用于简化 HTTP API 的调用。当用户在使用 Feign 客户端进行 HTTP 请求时遇到 `feign.FeignException$NotFound: status 404` 错误时,通常表示请求的目标资源未找到。以下是可能的原因及解决方法: #### 1. 检查服务路径是否正确 错误信息表明客户端尝试通过 Feign 调用的服务路径不存在或不匹配。需要确保 Feign 接口定义的 URL 和实际服务提供方的路径一致[^1]。例如: ```java @FeignClient(contextId = "exampleService", value = "example-service") public interface ExampleService { @PostMapping("/api/example") String exampleMethod(@RequestBody ExampleRequest request); } ``` 如果服务的实际路径为 `/api/v1/example`,而 Feign 接口定义为 `/api/example`,则会导致 404 错误。 #### 2. 网关转发配置问题 如果项目中使用了网关(如 Spring Cloud Gateway 或 Zuul),需要检查网关的路由配置是否正确。例如,网关可能未正确将请求转发到目标服务[^3]。以下是一个典型的网关配置示例: ```yaml spring: cloud: gateway: routes: - id: example_service_route uri: lb://example-service predicates: - Path=/api/example/** ``` 确保网关的 `Path` 配置与 Feign 客户端的请求路径匹配。 #### 3. 请求方法与参数问题 错误信息中提到的是 POST 请求,需要确认请求体和请求头是否符合服务端的要求[^2]。例如,服务端可能要求特定的 `Content-Type` 或自定义头部字段。以下是一个 Feign 接口的示例: ```java @FeignClient(contextId = "thirdpartyApi", value = "thirdparty-api") public interface ThirdpartyApi { @PostMapping(value = "/inside/checkSkuSaleStateAndStockInside", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) Response<JSONArray> checkSkuSaleStateAndStockInside( @RequestBody CheckSkuSaleStateAndStockReq req, @RequestHeader("Authorization") String token); } ``` 确保请求体和头部字段与服务端需求一致。 #### 4. 服务注册与发现问题 如果使用了服务注册与发现(如 Eureka 或 Consul),需要确认目标服务是否已正确注册到注册中心,并且 Feign 客户端能够解析到正确的服务地址[^3]。可以通过以下方式验证: - 检查注册中心的管理页面,确认目标服务的状态。 - 使用 `curl` 或 Postman 直接调用目标服务的接口,验证其可用性。 #### 5. 鉴权与 Token 问题 如果目标服务需要鉴权(如 OAuth2 或 JWT),需要确保 Feign 请求中携带了有效的 Token[^4]。例如: ```java @Configuration public class FeignConfig { @Bean public RequestInterceptor requestTokenBearerInterceptor() { return requestTemplate -> { String token = getTokenFromSomewhere(); requestTemplate.header("Authorization", "Bearer " + token); }; } } ``` 如果 Token 无效或过期,可能会导致请求被拦截并返回 404 错误。 ### 示例代码:Feign 客户端完整实现 以下是一个完整的 Feign 客户端实现示例,包含路径、请求头和请求体的定义: ```java @FeignClient(name = "example-service", configuration = FeignConfig.class) public interface ExampleServiceClient { @PostMapping(value = "/api/example", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) ResponseEntity<ExampleResponse> exampleMethod(@RequestBody ExampleRequest request); } @Configuration public class FeignConfig { @Bean public RequestInterceptor requestInterceptor() { return requestTemplate -> { String token = "your-valid-token"; requestTemplate.header("Authorization", "Bearer " + token); }; } } ``` ### 结论 `feign.FeignException$NotFound: status 404` 错误通常是由于路径不匹配、网关配置错误、请求参数不符合要求或鉴权失败等原因引起的。逐一排查上述问题即可定位并解决问题。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值