分析下gateway feign调用报错:No qualifying bean of type 'org.springframework.boot.autoconfigure.http.HttpMessageConverters'
时间: 2024-01-08 18:37:49 浏览: 191
这个错误通常是由于在使用 Feign 进行接口调用时,没有正确配置 HttpMessageConverters 导致的。HttpMessageConverters 是 Spring Boot 中的一个组件,用于将请求和响应的数据转换为特定的格式,例如将 JSON 数据转换为 Java 对象或将 Java 对象转换为 JSON 数据。
要解决这个问题,可以尝试以下几个步骤:
1. 确保在应用程序中引入了正确的依赖,包括 Feign 和 HttpMessageConverters。
2. 确定 Feign 接口调用中的请求和响应类型,并为其配置正确的 HttpMessageConverters。这可以使用 @Configuration 注解的类来完成,该类需要实现 FeignClientsConfiguration 类,并覆盖该类中的方法。
3. 检查应用程序的配置文件,确保没有覆盖默认的 HttpMessageConverters 配置。
如果上述步骤都无法解决问题,可以尝试在 Feign 接口调用中使用 @RequestBody 和 @ResponseBody 注解来明确指定请求和响应数据的格式。
相关问题
package com.github.wxiaoqi.security.gate.config; import feign.codec.Decoder; import org.springframework.beans.BeansException; import org.springframework.beans.factory.ObjectFactory; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.openfeign.support.ResponseEntityDecoder; import org.springframework.cloud.openfeign.support.SpringDecoder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.client.RestTemplate; import org.springframework.web.reactive.function.client.WebClient; import java.util.ArrayList; import java.util.List; /** * @author ace * @create 2019/1/27. */ @Configuration public class GatewayConfig { /** * 负载均衡注解 * @return */ @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } @Bean public Decoder feignDecoder() { return new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter())); } public ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() { final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(new GateWayMappingJackson2HttpMessageConverter()); return new ObjectFactory<HttpMessageConverters>() { @Override public HttpMessageConverters getObject() throws BeansException { return httpMessageConverters; } }; } public class GateWayMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter { GateWayMappingJackson2HttpMessageConverter(){ List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")); setSupportedMediaTypes(mediaTypes); } } @Bean @LoadBalanced public WebClient.Builder loadBalancedWebClientBuilder() { return WebClient.builder(); } }
这是一个 Spring Cloud Gateway 的配置类,其中包含了一些 Bean 的定义,例如负载均衡的 RestTemplate 和 WebClient.Builder。此外,还定义了一个 GateWayMappingJackson2HttpMessageConverter 类,用于处理响应报文的转换。同时,该类还使用了 Feign 的 Decoder 进行响应解码。
java.lang.IllegalStateException: Failed to execute CommandLineRunner at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:779) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] at com.unkown.data.hw.ipran.straight.collect.UnkownDataHwIpranStraightCollectApplication.main(UnkownDataHwIpranStraightCollectApplication.java:39) [classes/:na] Caused by: feign.FeignException$InternalServerError: [500 INTERNAL SERVER ERROR] during [POST] to [http://ants-flask/api/collect/endpoint] [RemoteIpranScanService#endpoint(String)]: [<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>TypeError: unsupported operand type(s) for +: 'int' and 'str' // Wer... (20567 bytes)] at feign.FeignException.serverErrorStatus(FeignException.java:231) ~[feign-core-10.10.1.jar:na] at feign.FeignException.errorStatus(FeignException.java:180) ~[feign-core-10.10.1.jar:na] at feign.FeignException.errorStatus(FeignException.java:169) ~[feign-core-10.10.1.jar:na] at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:92) ~[feign-core-10.10.1.jar:na] at feign.AsyncResponseHandler.handleResponse(AsyncResponseHandler.java:96) ~[feign-core-10.10.1.jar:na] at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138) ~[feign-core-10.10.1.jar:na] at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89) ~[feign-core-10.10.1.jar:na] at com.alibaba.cloud.sentinel.feign.SentinelInvocationHandler.invoke(SentinelInvocationHandler.java:107) ~[spring-cloud-starter-alibaba-sentinel-2.2.3.RELEASE.jar:2.2.3.RELEASE] at com.sun.proxy.$Proxy131.endpoint(Unknown Source) ~[na:na] at com.unkown.data.hw.ipran.straight.collect.controller.RTrsHwIpranCircuitController.IpranSend(RTrsHwIpranCircuitController.java:94) ~[classes/:na] at com.unkown.data.hw.ipran.straight.collect.MyRunner.run(MyRunner.java:22) ~[classes/:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE] ... 5 common frames omitted
这个错误是由于在执行CommandLineRunner时出现了异常。具体原因是发生了500内部服务器错误,原因是POST到http://ants-flask/api/collect/endpoint的RemoteIpranScanService#endpoint(String)出现了问题。在调用com.unkown.data.hw.ipran.straight.collect.controller.RTrsHwIpranCircuitController.IpranSend(RTrsHwIpranCircuitController.java:94)和com.unkown.data.hw.ipran.straight.collect.MyRunner.run(MyRunner.java:22)时也出现了问题。建议检查代码中的POST请求和响应处理是否正确,或者检查远程服务器是否正常。
阅读全文