Reputation: 95
I was trying my hand at writing a sample Spring Boot application.
My initial thought was it's an easy example to follow and write but I am not able to print a simple statement upon hitting my endpoint.
I don't want to use any view (HTML,JSP etc) right now as I want to learn the backend first.
I have followed:
However, still stuck after a whole day of googling.
So let me reiterate the steps, I did.
spring.io
and downloaded zip
.Tomcat 9.0.56
is present on my system.http://localhost:8080/greet
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@GetMapping("/greet")
public String sayHelloWorld() {
return "Hello World";
}
}
Spring Boot Application:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TodoApplication {
public static void main(String[] args) {
SpringApplication.run(TodoApplication.class, args);
}
}
pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sample</groupId>
<artifactId>todo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>todo</name>
<description>Back-end for To DO App</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
After navigating greet
URL:
Got the following exception at console:
Upvotes: 3
Views: 7571
Reputation: 373
I found yet one more way to get obtain a 404 with Eclipse, Spring Boot hello world application, and Maven. If you choose to 'Run on Server' or Maven install, that won't work. As the answer by Sengar already indicates, Run as, Maven Configuration, with the goal set to spring-boot:run, that will work. This answer is only to say that 'Run as Java Application' also works. But more importantly, 'Run on Server' won't, even though the war is exploded in the .metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ directory.
Upvotes: 0
Reputation: 95
So the solution as pointed out by xerx593 is below:
Also I have noticed that 8080 port is listening but spring boot fails to register on it, in this case set
server.port = port other than 8080
in application.properties.
Upvotes: 2