[英]spring boot RestController from maven dependency does not work
我的微服務核心項目中有一個以下控制器:
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")
...
}
現在,我正在初始化產品服務應用程序,如下所示:
@SpringBootApplication
public class ProductServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProductServiceApplication.class, args);
}
}
microservices-core中的類在product-service中可用。 但是運行產品服務時,我無法獲取localhost:8080 / version。 有人可以幫忙嗎?
我的猜測是您的主要應用程序類包與控制器類不在同一個包中。
將ComponentScan
批注添加到您的主類中,以掃描所有子包中的組件:
@SpringBootApplication
@ComponentScan({"com.XYZ.microservices.core"})
public class ProductServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProductServiceApplication.class, args);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.