我在localhost上运行两个spring boot应用程序。它们在不同的端口上运行。让我们称它们为SBA1 (Spring Boot App)和SBA2。在SBA2中有一个我需要使用的端点。我已经直接在SBA2的swagger UI上测试过了,我知道它可以工作。但是当我尝试使用SBA1中的端点时,我似乎无法调用它。这是我到目前为止尝试过的方法,
这是调用调用SBA2端点的类的服务:
@Service
public class HierarchyServiceImpl implements HierarchyService {
@Autowired
private PolicyRepository service;
//this is the class that calls SBA2's end point
@Autowired
private RuleEngineApi api;
@Override
public Policy calculateCollection(Collection collection) {
Policy policy = service.getPolicyData(collection.getPolicyNumber());
CollectionMapper mapper = new CollectionMapper();
Rule facts = new Rule();
facts.setFacts(mapper.mapCollections(collection, policy));
Rule rule = api.analyzeRules(facts);
return policy;
}
}这是调用SBA2的端点的类:
@FeignClient(name = "rule-engine-service", url = "http://localhost:8080")
public interface RuleEngineApi {
@PostMapping(value = "/v1/rule/analyzer", consumes = "application/json")
public Rule analyzeRules(Rule rule);
}这种方法的问题是应用程序无法定位RuleEngineApi类的bean。当我尝试运行SBA1时,它会显示以下内容:
required a bean of type '<path of class>.RuleEngineApi' that could not be found.\r\n\r\n\r\nAction:\r\n\r\nConsider defining a bean of type '<path of class>.RuleEngineApi' in your configuration.我试过这样的方法:
RuleEngineApi api = null;
Rule rule = api.analyzeRules(facts);当然,这会返回一个NullPointerException。我只想强调一下,SBA2运行得很好。当它们(SBA1和SBA2)都在本地运行时,我在调用它的端点时遇到了问题。任何帮助都将不胜感激
发布于 2020-07-21 14:27:56
SBA1永远不会创建RulesEngineApi的实例。我怀疑spring不会扫描这个包,但是如果不看包名和配置就很难判断。
发布于 2020-07-22 09:48:54
我一直试图弄清楚这一点,但似乎根本没有问题。我一直在尝试从Eclipse运行我的应用程序。显然,当我在终端上使用mvn spring-boot:run命令运行它时,一切都运行得很好。
当我在Eclipse上运行它时,我还没有找出问题出在哪里,但至少我的应用程序可以像预期的那样运行
发布于 2020-07-22 09:57:53
您需要配置@EnableFeignClient Annotation,还可以设置客户端所在位置的基包。像这样的东西
@EnableFeignClients(basePackages = {"my.external.feign.client.package", "my.local.package"})
https://stackoverflow.com/questions/63005843
复制相似问题