【问题标题】:spring boot RestController from maven dependency does not work来自maven依赖项的spring boot RestController不起作用
【发布时间】:2017-04-23 03:54:58
【问题描述】:

我的微服务核心项目中有以下控制器:

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。有人可以帮忙吗?

【问题讨论】:

  • 您遇到的错误是什么?第404章?
  • 是的,whitelabel 错误页面
  • 能否添加启动应用程序时在输出中看到的弹簧轨迹?

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


【解决方案1】:

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

在你的主类中添加ComponentScan注解来扫描所有子包中的组件:

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

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

【讨论】:

  • 就是这样。先生非常感谢您! :)
猜你喜欢
  • 1970-01-01
  • 2020-08-28
  • 1970-01-01
  • 2018-05-01
  • 1970-01-01
  • 2021-05-15
  • 2017-07-04
  • 2019-06-08
  • 2017-04-09
相关资源
最近更新 更多