无法写入请求:没有找到适合带有 Feign 的请求类型的 HttpMessageConverter
Posted
技术标签:
【中文标题】无法写入请求:没有找到适合带有 Feign 的请求类型的 HttpMessageConverter【英文标题】:Could not write request: no suitable HttpMessageConverter found for request type with Feign 【发布时间】:2021-07-26 07:51:44 【问题描述】:我想使用 Spring Cloud Feign 客户端实现请求。我试过这个:
Feign 客户端:
@FeignClient(name = "mail-service", url = "mail-service")
public interface EmailClient
@RequestMapping(method = RequestMethod.POST, value = "/register")
void setUserRegistration(RegisterUserDTO registerUserDTO);
@RequestMapping(method = RequestMethod.POST, value = "/password_reset")
void setUserPasswordReset(PasswordResetDTO passwordResetDTO);
请求 DTO:
public class RegisterUserDTO
private int id;
public RegisterUserDTO()
控制器:
@Autowire
EmailClient emailClient;
@PostMapping("/dummy")
public ResponseEntity<?> test()
RegisterUserDTO obj = new RegisterUserDTO();
emailClient.setUserRegistration(obj);
return ok().build();
Feign 配置:
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
eureka:
client:
serviceUrl:
defaultZone: $EUREKA_URI:http://localhost:8761/eureka
instance:
preferIpAddress: true
但是当我发出 POST 请求时,我得到了异常:
22:50:05.325 [http-nio-8090-exec-1] DEBUG DispatcherServlet[logResult:1101] - Failed to complete request: feign.codec.EncodeException: Could not write request: no suitable HttpMessageConverter found for request type [org.engine.plugin.dto.RegisterUserDTO]
22:50:05.328 [http-nio-8090-exec-1] DEBUG HstsHeaderWriter[writeHeaders:169] - Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@461d6969
22:50:05.329 [http-nio-8090-exec-1] DEBUG SecurityContextPersistenceFilter[doFilter:119] - SecurityContextHolder now cleared, as request processing completed
22:50:05.334 [http-nio-8090-exec-1] ERROR [dispatcherServlet][log:175] - Servlet.service() for servlet [dispatcherServlet] in context with path [/engine] threw exception [Request processing failed; nested exception is feign.codec.EncodeException: Could not write request: no suitable HttpMessageConverter found for request type [org.engine.plugin.dto.RegisterUserDTO]] with root cause
feign.codec.EncodeException: Could not write request: no suitable HttpMessageConverter found for request type [org.engine.plugin.dto.RegisterUserDTO]
你知道我该如何解决这个问题吗?
【问题讨论】:
尝试将 getter 和 setter 添加到您的 POJO RegisterUserDTO。 我添加了 getter/setter 但我仍然得到同样的错误。 这就是问题所在。谢谢! 【参考方案1】:在我的例子中,我使用了 openfeign
依赖,而没有 spring-boot-starter-web
。添加此依赖项后,错误消失。可能是因为它包含所需的 json 映射器。
【讨论】:
【参考方案2】:是的,您没有在 DTO 中添加 getter 方法。
public class RegisterUserDTO
private int id;
public RegisterUserDTO()
public int getId()
return id;
查看我添加的代码。
【讨论】:
以上是关于无法写入请求:没有找到适合带有 Feign 的请求类型的 HttpMessageConverter的主要内容,如果未能解决你的问题,请参考以下文章
带有 List 参数的 Spring Cloud Feign Client @RequestParam 创建错误的请求
org.springframework.web.servlet.PageNotFound - 没有为带有 URI 的 HTTP 请求找到映射