Spring boot - Hello world

18 阅读1分钟

一、在IDEA中新建一个maven工程file->new - Project

image.png

二、此时创建的目录结构如下

image.png

三、配置maven:file -> settings

image.png

四、配置下编码格式

image.png

五、pom文件中导入依赖

<?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>
    <!--导入的依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
    </parent>
    <groupId>org.example</groupId>
    <artifactId>sbpro</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <!--导入的依赖-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

六、增加主启动类

image.png

项目成功启动

image.png

七、新建测试类测试

image.png

启动工程后,浏览器成功访问

image.png

八、更改访问端口

默认的端口是8080,我们可以主动更改端口。在resources目录下新建application.yml文件。并配置新的端口号。

image.png

重新启动,浏览器访问

image.png