Skip to content
Advertisement

Spring Boot returns Error 404 even though mapping has been set

I downloaded a Spring Boot project from Spring Initializr. I am trying to call sayHello() method in DemoApplication.java. Here are my code.

DemoApplication.java

JavaScript

pom.xml

JavaScript

When I run on http://localhost:8080/hello it returns error 404 page. It does not call sayHello() method even though I have set the GET mapping. What could be the issue?

Advertisement

Answer

The problem is your dependencies, or rather the lack of them.

You included spring-web as a dependency but that isn’t enough to launch your web app. You would also need to add spring-webmvc and as you want to use REST (and probably JSON) you would need to add the jackson-databind dependency (and if you want to support dates also the jackson-datatype-jdk8 dependency).

Now you could of course manually figure all this out yourself by trial and error. Or you just include the spring-boot-starter-web which automatically includes (compatible) versions of all of those dependencies (including Embedded Tomcat).

So in short fix your dependencies.

JavaScript

This should be all you need, if you build a WAR instead of JAR you might want to add

JavaScript

This will make tomcat provided and moved out of the lib folder but still your app would be runnable for development (through the main method) and deployable as a war (without tomcat being in the lib directory).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement