Jacobvu84 / developer-career

Study for interview
1 stars 0 forks source link

Day 01: Spring Initializr: API #3

Open Jacobvu84 opened 2 years ago

Jacobvu84 commented 2 years ago

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

./mvnw install

Run Spring Boot App with java command

java -jar target/app-demo-0.0.1-SNAPSHOT.jar

Or Run Spring Boot App with Maven

mvn spring-boot:run

Adding 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>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
Jacobvu84 commented 2 years ago

2. Explaining some annotations

@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]

Jacobvu84 commented 2 years ago

[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";
    }

}
Jacobvu84 commented 2 years ago