IDEA 2022.3.3 Create SpringBoot project

Table of contents

Step 01: Quickly create a project

Step 02: Select dependencies

Step 03: Version problem in pom file

Step 04: Start testing

4.1. Understanding the guidance category

4.2. Create Controller class for testing

Possible problems and solutions

Attachment 1: pom file source code

Attachment 2: Project structure chart


Quickly create a springBoot project on the Chinese version of IDEA 2022.3.3.

It should be noted that this feature may not exist on the community version of IntelliJ IDEA.

The creation steps are as follows.

Step 01: Quickly create a project

Select Spring Initializr when creating the project, as shown in the figure below:

Step 02: Select dependencies

as the picture shows. Select the dependencies that need to be added to the project, and then IDEA will automatically add the selected dependencies to the project's pom.

It should be noted that the current case uses JDK 8, so the Spring Boot version needs to be adjusted to 2.xx.

 Spring Boot version number:

  • Pay attention to the choice of SpringBoot version. If using JDK8, choose version 2.xx.
  • Spring official website introduction: If you choose SpringBoot version 3.0.0, the minimum JDK version is 17
  • Failure to adapt will cause the Application to fail.

  • Snapshot is a concept in the engineering version, which means a snapshot version, which is a relatively stable version, but it will be improved, and it is best not to be used in the production environment.
  • In a production environment, release should be selected

 

Step 03: Version problem in pom file

After creating the project, modify the maven repository to a local repository.

At the same time, you need to wait for various files to be downloaded.

After creation, the project structure is:

question:

        After the creation is completed, you may encounter that the dependency spring-boot-maven-plugin in the pom.xml file is not imported. As shown below:

problem analysis:

        Because this dependency is not found in the Alibaba Cloud warehouse of maven.
problem solved:

        Find the version number of the parent project in the pom.xml file, copy it to the red dependency, and let it inherit it. As shown below:

Step 04: Start testing

4.1. Understanding the guidance category

 This class is automatically generated when creating a project. It is the startup class of SpringBoot project.

/**
 * spring boot 工程都有一个启动类,这是工程的入口
 * 并在引导类上添加注解 @SpringBootApplication
 * */
@SpringBootApplication
public class SpringBootTest01Application {
  
    public static void main(String[] args) {
        SpringApplication.run(SpringBootTest01Application.class, args);
    }

}

4.2. Create Controller class for testing

The boot class needs to be in the upper directory of the Controller class!

package cn.shq.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController //是 @Controller和 @ResponseBody的组合注解,返回的都是字符。
public class HelloController {
    @GetMapping("hello") //指定使用get方式的请求。
    public String Hello(){
        return "hello world !";
    }
}

 You can now start the project

 Browser access results:

 The project access is successful and subsequent programming can be performed.

Possible problems and solutions

Question 1:

        After starting the SpringBoot project, open the browser to access the tomcat server and the following situation occurs:

        Whitelabel Error Page (also called white page) is the explanation page for abnormal HTTP requests in SpringBoot.

problem analysis:

The real reason why springboot's white pages appear is mainly because there is no suitable matching situation and a 404 situation occurs. Then jump to the system's default first ErrorPage, which is the white page content.

Specific solution: Wait for subsequent updates.

The reference method currently provided : write the full path to the access address ------>localhost:8080/hello

Question 2:

        java: warning: source release 17 requires target release 17

Solution steps : Set the bytecode version in the java compiler to the corresponding JDK version in the settings.

 Modify the java compiled version in the pom file.

Question 3: 

        java: Unable to access org.springframework.boot.SpringApplication
        error class file:
/D:/tools/maven/apache-maven-3.6.3/respo/liuxinRespos/org/springframework/boot/spring-boot/3.0.0/ spring-boot-3.0.0.jar!/org/springframework/boot/SpringApplication.class

        The class file has the wrong version 61.0, it should be 52.0.

        Please delete the file or ensure that the file is in the correct classpath subdirectory.

Reason: It is still because the version number of the parent project in the pom file is 3.xx, which does not match the JDK version used.

Solution steps  :

Attachment 1: pom file source code

<?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.7.12</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.shq</groupId>
    <artifactId>springBoot_test01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springBoot_test01</name>
    <description>springBoot_test01</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>
                <version>2.7.12</version>
            </plugin>
        </plugins>
    </build>

</project>

Attachment 2: Project structure chart

Project structure diagram currently used as a test project. 

Guess you like

Origin blog.csdn.net/woshishq1210/article/details/130955798