How to call endpoints on two spring boot application on running on localhost

Viewed 285

I'm running two spring boot applications on localhost. They are running on different ports. Let's call them SBA1 (Spring Boot App) and SBA2. There's an end point in SBA2 that I need to use. I already tested it directly on SBA2's swagger UI and I know its working. But when I try to use said end point from SBA1, I can't seem to call it. Here's what I tried so far,

This is the service that calls the class which calls the SBA2's endpoint:

@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;
    }

}

This is the class that calls SBA2's endpoint:

@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);
}

The problem with this approach is the application is unable to locate the bean for the RuleEngineApi class. It says this when I try to run 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.

I tried something like:

RuleEngineApi api = null;
Rule rule = api.analyzeRules(facts);

But of course that returned a NullPointerException. I would just like to emphasize that SBA2 is working fine. I'm just having trouble calling its endpoint when they (SBA1 and SBA2) are both running locally. Any help would be much appreciated

3 Answers

SBA1 is never creating an instance of RulesEngineApi. I suspect spring isn't scanning that package, but hard to tell without seeing package names and configs.

I've been trying to figure this out but it seems there was no problem at all. I've been attempting to run my application from Eclipse. Apparently when I run it from a terminal with the mvn spring-boot:run command, everything was working fine.

I haven't figure out what's wrong when I run it on Eclipse, but at least my application is running as expected

You need to configure @EnableFeignClient Annotation and you can also set the base package for where you're clients resides in. Some thing like this

@EnableFeignClients(basePackages = {"my.external.feign.client.package", "my.local.package"})

Related