How to resolve 'Resource not found 404 NOT_FOUND' when running spring boot program
Problem
When we run a Spring Boot program and visit a URL as follows:
Sometimes, we get this error:
Why does this error happen? The program is correct, I promise!
Environment
- Spring Boot 1.x and 2.x
The code
The TheController.java mapping code of the Spring Boot program is:
The structure of the project:
Reason
After the adminHome
method, the controller returns a "home_admin"
view, which cannot be resolved by Spring Boot.
Solution
We should change the mapping to make it the same name as the view name:
The key point is:
It should have the same name as the view name:
Run the app again, and no error messages will appear. It works!
Summary
In this post, we explored how to resolve the Resource not found 404 NOT_FOUND
error in Spring Boot. The main issue arises when the URL mapping in the @GetMapping
annotation does not match the view name returned by the controller. By ensuring that these names match, we can avoid this common error. This solution is applicable to both Spring Boot 1.x and 2.x versions.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!