You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot报错:No bean named 'myController' available,HelloWorld程序求助

Fixing "No bean named 'myController' available" Error in Your Spring Boot Hello World App

Hey there, let's break down why you're hitting this error and how to fix it step by step!

1. The Root Cause: Spring Can't Find Your Controller Class

Spring Boot automatically scans for components (like @Controller, @Service, etc.) starting from the package where your main application class (DemoApplication) lives, plus all its sub-packages.

Looking at your code:

  • DemoApplication sits in the di.prac package
  • MyController is in the standalone controllers package

Since controllers isn't a sub-package of di.prac, Spring has no idea this class exists—so it never creates a bean for it, hence the error.

Fixes for Package Scanning

You have two straightforward options here:

  • Option 1: Move the controller to a valid sub-package
    Relocate MyController.java to di.prac.controllers (just create the controllers sub-package under di.prac). This way, Spring's default scan will pick it up automatically without extra configuration.

  • Option 2: Explicitly tell Spring which packages to scan
    Update your DemoApplication to use the scanBasePackages attribute in @SpringBootApplication, specifying both your main app package and the controllers package:

    package di.prac;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication(scanBasePackages = {"di.prac", "controllers"})
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    

2. Add Request Mapping to Your Controller Method

Even once Spring finds your controller, your hello() method won't work as expected without a request mapping annotation. Right now, Spring doesn't know which HTTP requests this method should handle.

Update your MyController to add a @GetMapping (or @RequestMapping) annotation:

package controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MyController {
    @GetMapping("/hello") // Maps GET requests to /hello to this method
    public String hello() {
        System.out.println("Hello World");
        return "foo";
    }
}

Quick Note: @Controller vs @RestController

If you want to return plain text (like "Hello World") directly as the HTTP response instead of looking for a view template named "foo", swap @Controller with @RestController:

package controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    @GetMapping("/hello")
    public String hello() {
        System.out.println("Hello World");
        return "Hello World"; // Sends this string as the response body
    }
}

Final Checks

After making these changes, restart your Spring Boot app. You should now be able to access the /hello endpoint (via browser or tools like Postman) and see "Hello World" in your console (and in the response if you used @RestController).

内容的提问来源于stack exchange,提问作者Ankit Halder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:39:33