Spring Boot OpenFeign 动态切换 Url 报错解决方法
Spring Boot OpenFeign About 1,009 words错误信息
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer
错误代码
为了能动态切换路径编写了如下代码,@FeignClient
注解中没有配置url
属性。
通过@GetMapping
透传路径。
@FeignClient(name = "my-feign-client")
public interface Router {
@GetMapping("{path}")
@ResponseBody
ResponseEntity<JsonNode> get(@PathVariable(value = "path") String path,
@SpringQueryMap MultiValueMap<String, String> queryString);
}
错误原因
在没有配置负载均衡的情况下,必须指定url
路径,且不能为空。
解决方法
url
参数随意指定一个占位符即可,在方法参数中传入URI
对象。
@FeignClient(name = "my-feign-client", url = "placeholder")
public interface Router {
@GetMapping("{path}")
@ResponseBody
ResponseEntity<JsonNode> get(URI uri, @PathVariable(value = "path") String path,
@SpringQueryMap MultiValueMap<String, String> queryString);
}
使用
@Resource
private Router router;
URI uri = URI.create(url);
ResponseEntity<JsonNode> response = router.get(uri, path, queryString);
Views: 1,706 · Posted: 2023-07-12
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...