SpringBoot web项目初始化

1.新建maven项目后在pom.xml文件里加入下面的依赖

<!--SpringBoot父工程,版本号2.5.0-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
    </parent>

    <!--SpringBoot Web场景启动器-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

2.新建HelloApplication.java启动类

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

@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }
}

3.在controller包下新建IndexController.java控制器

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

@RestController
public class IndexController {

    //访问/或/index路径
    @RequestMapping({"/","/index"})
    public String index(){
        return "hello world";
    }
}

4.在resources目录下新建application.properties配置文件

#服务端口
server.port=8001

目录结构如下图:

启动项目访问http://localhost:8001/成功