Beginners to build a spring boot project

1. Open Idea and create a new maven project. See the figure below for details (idea version 2021.1.2)

 

2. Check whether the maven package of the new project exists,

 

 Note: The maven package needs to be downloaded by yourself, pay attention to download the package that matches the version

3. Import the dependencies related to spring boot, and replace the content of the pom.xml file of the new project with the following content. For details, please refer to the notes.

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springHello</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.5.9.RELEASE</version>

    </parent>

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

    </dependencies>
<!--    这段配置的作用是可以把项目直接打成一个可执行的jar包,不需要再打成war包-->
    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins>

    </build>
</project>

 4. Write a spring boot main program to start the project

 ! ! ! Create a new package here because the main program must be under a certain package

 Replace the code in the newly created class with the following code:

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @SpringBootApplication用来标注一个主程序,说明这是一个springboot应用
 */
@SpringBootApplication
public class SpringBootMain {
    public static void main(String[] args) {
        //让spring应用启动起来
        SpringApplication.run(SpringBootMain.class,args);
    }
}

 5. Write a controller for testing

Create a new package controller

 Create a new class testController under the package

Copy the following code into the controller class:

package com.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class testController {
    /**
     * @RequestMapping标注接受来自浏览器的请求
     * @ResponseBody用于响应请求
     */
    @ResponseBody
    @RequestMapping("/hello")
    public String String(){
        return "hello world";
    }
}

 6. Start the main program

 Successful access to the background:

 You can see the port number in the startup log

 

Guess you like

Origin blog.csdn.net/psjasf1314/article/details/128440280