Today I suddenly want to write a Hello world to spring boot, after the application successfully started, access to the Http://localhost:8080/hello newspaper 404.
Scenario Description: Controller class Package: Com.yjl.test.controller,,, Spring boot App startup class package: Com.yjl.test.springboot, according to the online search spring The boot application scanning class, so in the application launch class plus @componentscan (Basepackages = {"Com.yjl.test.controller.*"}), the project started successfully, is still reported 404, The s.w.s.m.m.a.requestmappinghandlermapping in the console log also does not have a mapped mapping of "/hello".
Solve:
The class for the spring boot default Scan project is the current package and sub-package of the startup class , and the package that the Controller class belongs to must be a child package of the package that the startup class belongs to. (although there are classes of start-up classes and controllers that can be placed under different packages and then use Componentscan annotations, I did not pass the test ....) )
Place the spring boot startup class under the Com.yjl.test package, and then remove @componentscan (basepackages = {"Com.yjl.test.controller.*"}), the project starts successfully, and the log appears:
S.w.s.m.m.a.requestmappinghandlermapping:mapped "{[/hello],methods=[get]}" onto public java.lang.String Com.yjl.test.controller.TestController.getHello ()
Run successfully ...
Spring boot instance report 404 issues