I am learning Spring Boot but I am not able to execute the basic "Hello World" program. I have built the basic project using https://start.spring.io/ and updated the code as per below code snippets:
Spring Boot Main Class:
package com.rohit.springboot.practice.selfspringboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.rohit.springboot.practice.selfspringboot")
public class SelfSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SelfSpringBootApplication.class, args);
}
}
Controller Class:
package com.rohit.springboot.practice.selfspringboot;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ThisWorldSpringContainer {
@RequestMapping(method = RequestMethod.GET, value = "/hello-world")
public String getWelcome() {
return "Hello World";
}
}
POM.xml 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.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.rohit.springboot.practice</groupId>
<artifactId>self-spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>self-spring-boot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</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>
Error:
Message: The requested resource [/hello-world] is not available
I know this question has been asked by multiple people but i have gone through those answers and tried those given answers but didn't work.
Technologies used are Java 8, Tomcat 9, Spring Boot 2.6.6 etc.
Kindly help.
CodePudding user response:
Since you don't have the Tomcat dependency, I'm implying that you want your application to run in a standalone Tomcat instance. Also, given your comment, I believe you're working on either Eclipse or Spring Tools Suite (Which is the same, really).
With that, it seems your problem is the lack of a packaging method. Tomcat needs the code its going to run in a package. This package is a zip-like file with .war
extension. Maven, the program that handles the dependencies of your project according to the pom.xml
file, can package it in either war
of jar
format. Since you are running it in an external Tomcat, you want the war
extension. So please, add the following line to your pom.xml
file. It has to be in the same level as <name>
:
<packaging>war</packaging>
Then, update your project (Alt F5, check your project and hit OK) then try running your project again. You should see an stylish "SPRING" output in your console.
Now, since you are running this externally, you need to specify the name of the project, so you should call your endpoint with
http://localhost:8080/<your_project_name_here>/hello-world
CodePudding user response:
I have modified your code as below:
@RequestMapping
@RestController
public class ThisWorldSpringContainer {
@GetMapping(value = "/hello-world")
public String getWelcome() {
return "Hello World";
}
}
I thinking you are missing
@Requestmapping
in classlevel