Issue
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.
- Initialized a spring boot project from
spring.io
and downloadedzip
. - Extracted the zip and imported it into Eclipse.
- Configured a Rest Controller with an endpoint to just print a hello world statement.
Tomcat 9.0.56
is present on my system.- Running TodoApplication as Java application or Spring boot.
- URL I am hitting is
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:
Solution
So the solution as pointed out by xerx593 is below:
- Make sure to select packaging as war/jar in https://start.spring.io/.
- Import the zip file.
- Add your controller class in same or child package of SpringBootApplication, refer this for more info https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.structuring-your-code.
- Open terminal and change directory to working folder.
- Run mvn spring-boot:run if using maven.
- Hit the endpoint for example http://localhost:8080/greet or curl request.
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.
Answered By - Harsh Sengar
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)