hrishikeshp19
hrishikeshp19

Reputation: 9036

spring boot RestController from maven dependency does not work

I have a following controller in my microservices-core project:

package com.XYZ.microservices.core.api.version;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/version")
public class VersionController {

    @Autowired
    private VersionService versionService;

    @RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
    public Version getVersion() {
        return versionService.getVersion();
    }
}

I have another project called product-service. I am importing microservices-core to product-service like this:

dependencies {
        compile("com.XYZ:microservices-core:1.0.0-RELEASE")
        ...
}

Now, I am initializing product-service application like this:

@SpringBootApplication
public class ProductServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductServiceApplication.class, args);
    }
}

The classes in microservices-core are available in product-service. But I am not able to GET localhost:8080/version when I run product-service. Can someone help?

Upvotes: 0

Views: 2917

Answers (1)

Strelok
Strelok

Reputation: 51481

My guess is your main application class package is not in the same package as the controller class.

Add ComponentScan annotation to your main class to scan all subpackages for components:

@SpringBootApplication
@ComponentScan({"com.XYZ.microservices.core"})
public class ProductServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductServiceApplication.class, args);
    }
}

Upvotes: 3

Related Questions