Open Jacobvu84 opened 2 years ago
@SpringBootApplication
Annotation: Using to mark which class is Main Class Application - docs. Main class is the place where to configure and boot
In order to run a Spring Boot application, it needs to have a class with the @SpringBootApplication annotation. The following code demonstrates this:
package jacob.vu.coffee;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Welcome {
public static void main(String[] args) {
SpringApplication.run(Welcome.class, args);
}
}
What if we have 2 classes with the same the annotation.
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.7.3:run (default-cli) on project frist-web: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:2.7.3:run failed: Unable to find a single main class from the following candidates [jacob.vu.coffee.Welcome, jacob.vu.coffee.SprintBootStudyApplication] -> [Help 1]
[Issue] This application has no explicit mapping for /error
You should see this error when running application first time.
We need to create a Controller for mapping
package jacob.vu.coffee;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class IndexController implements ErrorController{
private final static String PATH = "/error";
@RequestMapping(PATH)
@ResponseBody
public String getErrorPath() {
// TODO Auto-generated method stub
return "No Mapping Found";
}
}
Docs guide: https://github.com/spring-io/start.spring.io/blob/main/USING.adoc
1. When generating demo project completed. Run this command to build
Run Spring Boot App with java command
Or Run Spring Boot App with Maven
Adding dependencies