FeignClient【问题】Method threw ‘feign.codec.DecodeException‘ exception.也许是最简单的解决方法

简介: FeignClient【问题】Method threw ‘feign.codec.DecodeException‘ exception.也许是最简单的解决方法

1.问题详情

Method threw 'feign.codec.DecodeException' exception.
# detail
Could not extract response: 
no suitable HttpMessageConverter found for response type 
[class java.lang.Object] and content type [text/json;charset=UTF-8]

Openfeign版本:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
  <version>3.0.3</version>
</dependency>

Feign接口代码:

@PostMapping(value = "{url}")
Object executePost(@PathVariable("url") String url, @RequestBody Object params);

feign.codec.DecodeException 异常表示在使用 Feign 进行解码(反序列化)响应时发生了错误。这种异常通常与响应内容的解析有关。

以下是一些可能导致 feign.codec.DecodeException 异常的原因和解决方法:

  1. 响应内容格式错误:检查服务端返回的响应内容是否符合预期的格式。例如,如果您期望的是 JSON 格式的响应,但实际返回的内容不是有效的 JSON 格式,会导致解析异常。请确保响应内容与您的预期相符。
  2. 缺少适当的解码器:Feign 需要配置适当的解码器来解析响应内容。如果使用的是 JSON 格式的响应,您可以添加 JacksonDecoder 或其他 JSON 解码器。
  3. 反序列化失败:如果响应内容的结构与您预期的类型不匹配,或者存在无法反序列化的字段或属性,会导致解码异常。请确保您的代码中定义的对象类型与实际的响应内容相匹配,并且可以正确地进行反序列化。
  4. 依赖版本不匹配:检查您的项目中 Feign 相关的依赖版本是否与您使用的 Feign 版本兼容。不匹配的依赖版本可能导致解码异常。请确保您的项目中所有相关的依赖版本正确配置。

2.问题解决

Feign接口代码调整:

@PostMapping(value = "{url}")
String executePost(@PathVariable("url") String url, @RequestBody Object params);

是不是很简单,实际接口返回的时JSON字符串。

目录
相关文章
|
12月前
|
Java 应用服务中间件 微服务
【Java异常】feign.FeignException: status 400 reading xxx 异常解决方案
【Java异常】feign.FeignException: status 400 reading xxx 异常解决方案
115 0
|
5月前
|
JSON Java 数据格式
RestTemplate请求UnknownContentTypeException:no suitable HttpMessageConverter异常
RestTemplate请求UnknownContentTypeException:no suitable HttpMessageConverter异常
236 0
|
Java Spring
【Spring常见错误】Initialization failed for ‘https://start.spring.io‘
📋📋 精彩摘要:新建SpringBoot项目时,使用官网连接创建项目报错: Initialization failed for 'https://start.spring.io' Please check URL, network and proxy settings. 本文将提供两种解决方式参考。
7734 0
【Spring常见错误】Initialization failed for ‘https://start.spring.io‘
Bad Request, Resolved [org.springframework.http.converter.HttpMessageNotReadableException,跟着视频仔细比对
Bad Request, Resolved [org.springframework.http.converter.HttpMessageNotReadableException,跟着视频仔细比对
|
5月前
|
SQL 关系型数据库 MySQL
c.t.t.handler.GlobalExceptionHandler : 捕获异常:org.springframework.jdbc.BadSqlGrammarException:
c.t.t.handler.GlobalExceptionHandler : 捕获异常:org.springframework.jdbc.BadSqlGrammarException:
42 0
SpringCloud Feign报错Method has too many Body parameters
SpringCloud Feign报错Method has too many Body parameters
|
5月前
|
前端开发 Java Spring
Spring MVC 出现Request method ‘GET‘ not supported解决办法
Spring MVC 出现Request method ‘GET‘ not supported解决办法
|
12月前
SpringCloud Feign报错:java.lang.IllegalStateException: Method has too many Body parameters
SpringCloud Feign报错:java.lang.IllegalStateException: Method has too many Body parameters
179 0
|
5月前
|
Java Maven Spring
SpringBoot运行出现 Lookup method resolution failed; nested exception is java.lang.IllegalStateException
SpringBoot运行出现 Lookup method resolution failed; nested exception is java.lang.IllegalStateException
1072 0
|
12月前
|
缓存 NoSQL Java
【异常】com.alicp.jetcache.CacheException: refresh error
【异常】com.alicp.jetcache.CacheException: refresh error
151 1