Spring Boot请求返回404错误求助:按教程操作仍未解决
Hey there! Let's dig into why you're hitting that frustrating 404 even though your server starts up fine. I spotted a couple of key issues in your code that are likely causing this:
1. Invalid Syntax in @ComponentScan
Looking at your main class, the @ComponentScan annotation has extra angle brackets around the package path:
@ComponentScan(basePackages = "<io.javabrains.springbootstarter.hello>")
Those <> are invalid syntax here—Spring can't parse the package name correctly, so it fails to scan and register your HelloController.
Fix it by removing the angle brackets:
@ComponentScan(basePackages = "io.javabrains.springbootstarter.hello")
2. Unnecessary @ComponentScan (Optional but Recommended)
Actually, the @SpringBootApplication annotation already includes @ComponentScan by default—it automatically scans the package where your main class lives and all its sub-packages.
If your HelloController is in the same package as CourseApiApp or a sub-package of it, you can completely remove the @ComponentScan annotation to simplify your code:
@SpringBootApplication public class CourseApiApp { public static void main(String[] args) { SpringApplication.run(CourseApiApp.class, args); } }
3. Double-Check Package Structure
Make sure your HelloController is placed in a package that Spring can scan. For example:
- If your main class is in
io.javabrains.springbootstarter, your controller should be inio.javabrains.springbootstarterorio.javabrains.springbootstarter.hello(a valid sub-package).
4. Verify the Request URL
After fixing the above, restart your server and confirm you're accessing the correct URL:http://localhost:8080/hello (assuming you're using the default port 8080—adjust the port if you changed it in application.properties).
Give these fixes a try, and your /hello endpoint should start returning "Hi" as expected!
内容的提问来源于stack exchange,提问作者Bartłomiej Matyjaszczyk

