Ch00 HelloWorld

4/16/2021 JavaSpring

# Hello World

尚硅谷笔记:https://www.yuque.com/atguigu/springboot

  • SpringBoot2 必须 java8以上,maven3.3以上

新建项目:

  • 导入依赖:

  • 父工程为springboot

  • 依赖为web开发

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

创建主方法:

image-20230425170228688

编写一个controller,接收/hello请求,并返回一个字符串(类似 springMVC)

image-20230425170244335

(专门用来返回字符串的controller可以用@RestController注解来修饰)

springboot简化配置,所有的配置都可以写在一个配置文件中:resource文件夹下的application.properties,可以参考官方文档

例如修改端口:

image-20230425170259047

springboot简化部署,不用像以前一样打包成war包,再部署到tomcat上,可以直接打包成jar包,并使用cmd运行:

在maven依赖中加入:

image-20230425170321700

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
1
2
3
4
5
6
7
8

如果起不来,记得取消掉cmd的快速编辑模式

image-20230425170349576

运行:

image-20230425170405409

linux运行项目:

image-20230425170416423

Last Updated: 11/19/2024, 1:54:38 PM