Spring启动来自maven依赖的RestController不起作用

时间:2016-12-08 01:45:46

标签: java spring gradle spring-boot dependency-management

我的微服务核心项目中有一个跟随控制器:

pax$ xx=4 ; if (( $(bc <<<"$xx < 5 || $xx > 7") )) ; then echo not 4-6 ; fi
not 4-6
pax$ xx=5 ; if (( $(bc <<<"$xx < 5 || $xx > 7") )) ; then echo not 4-6 ; fi
pax$ xx=9 ; if (( $(bc <<<"$xx < 5 || $xx > 7") )) ; then echo not 4-6 ; fi
not 4-6

我有另一个名为product-service的项目。我正在将microservices-core导入产品服务,如下所示:

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

现在,我正在初始化这样的产品服务应用程序:

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

microservices-core中的类可用于产品服务。但是当我运行产品服务时,我无法获得localhost:8080 / version。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

我的猜测是你的主应用程序类包与控制器类不在同一个包中。

ComponentScan注释添加到主类中以扫描组件的所有子包:

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

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