控制器方法不返回值

时间:2018-06-16 21:28:42

标签: spring spring-boot

我正在构建一个spring boot应用程序。这是我的RestController

@RestController
@RequestMapping("api/v1/bikes")
public class BikeController {


    @GetMapping 
      public  String s(){


        return " show this message";
        }

当我在POSTMAN中粘贴以下网址时,我收到以下消息 的 http://localhost:8080/api/v1/bikes

{
    "timestamp": "2018-06-16T21:19:17.791+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/api/v1/bikes"
}

为什么我收到错误?我希望看到方法s()返回的消息。

我的项目名称是自行车,它的结构在

之下
**bike
    -- src/main/java
                  --------- com.globomatics.bike (here is my main class)
                  --------- controllers**

这是我的主要课程

@SpringBootApplication
public class BikeApplication {

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

2 个答案:

答案 0 :(得分:0)

问题是您@RestController未扫描@SpringBootApplication。你可以在这做两件事。将控制器移动到与主类相同的包中,或指定要扫描其他包。像@SpringBootApplication(scanBasePackages = {"com.globomatics.*"})这样的东西。查看文档herehere,其中所有这些内容都得到了很好的解释。

答案 1 :(得分:0)

defult的

@SpringBootApplication 将始终扫描包含main方法的类和其中所有包的相同包。在您的情况下,包含主方法的类的包是" com.globomatics.bike"所以你的类 BikeController 应该在同一个包中,或者包含类 BikeController 的包必须在包" com.globomatics.bike"内。或者你可以通过使用注释来explcitly告诉扫描其他一些包 @ComponentScan("您想要春季扫描的包名称")。

将您的代码更改为:

import org.springframework.context.annotation.ComponentScan;
    @SpringBootApplication
    @ComponentScan("controllers")
    public class BikeApplication {

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