SpringBoot报错:No bean named 'myController' available,HelloWorld程序求助
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:
DemoApplicationsits in thedi.pracpackageMyControlleris in the standalonecontrollerspackage
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
RelocateMyController.javatodi.prac.controllers(just create thecontrollerssub-package underdi.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 yourDemoApplicationto use thescanBasePackagesattribute 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

