6.SpringBoot入门程序

 

SpringBoot入门程序

1. 环境要求

  1. JDK1.8
  2. maven3.x
  3. spring tools suite3.9
  4. springboot2.x版本
  5. spring5

2. 创建项目

2.1 方式1

https://start.spring.io/

再导入

2.2 方式2

3. 项目目录说明

resource/static/: 存放web的静态资源文件,如images, css, js等
resources/templates: 存放模板引擎的文件,类比于WEB项目的WEB-INF
application.properties: SpringBoot的核心配置文件
test/java/ApplicationTests.java: 测试文件

4. 项目依赖说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?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 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<!-- current project -->
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0</version>
<name>demo</name>
<description>Spring Boot demo</description>

<properties>
<java.version>1.8</java.version>
</properties>

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

<!-- SpringBoot test -->
<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>
</plugin>
</plugins>
</build>

</project>

5. 创建Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.example.demo.controller;

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

@RestController
public class HelloWorldController {

@RequestMapping("hello")
public String sayHello() {
return "Hello World!";
}
}

6. 启动项目

SpringBoot启动类右键->Run as->Java Application或者Spring Boot App

项目右键->Run as->Spring Boot App

1
2
mvn package
java -jar demo-1.0.jar

查看结果:

http://localhost:8080/hello

7. banner的修改

spring Boot启动的时候会有一个默认的启动图案。如下:

1
2
3
4
5
6
7
8
9

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.1)

(1)在src/main/resources路径下新建一个banner.txt文件,并输入想要的内容即可。

(2)在线生成图案的网站http://www.network-science.de/ascii/

将生成的图案粘贴进banner.txt文件,并启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 ____             _             ____              _   
/ ___| _ __ _ __(_)_ __ __ _| __ ) ___ ___ | |_
\___ \| '_ \| '__| | '_ \ / _` | _ \ / _ \ / _ \| __|
___) | |_) | | | | | | | (_| | |_) | (_) | (_) | |_
|____/| .__/|_| |_|_| |_|\__, |____/ \___/ \___/ \__|
|_| |___/

2021-01-10 19:23:16.503 INFO 12036 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 1.8.0_271 on DESKTOP-IOB28AF with PID 12036 (E:\SpringToolSuite\project\demo-4.8.1\demo\target\classes started by Tom in E:\SpringToolSuite\project\demo-4.8.1\demo)
2021-01-10 19:23:16.505 INFO 12036 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-01-10 19:23:17.317 INFO 12036 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-01-10 19:23:17.326 INFO 12036 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-01-10 19:23:17.326 INFO 12036 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-01-10 19:23:17.375 INFO 12036 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-01-10 19:23:17.375 INFO 12036 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 836 ms
2021-01-10 19:23:17.519 INFO 12036 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-01-10 19:23:17.660 INFO 12036 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-01-10 19:23:17.669 INFO 12036 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.439 seconds (JVM running for 1.759)

也可以关闭banner

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.example.demo;

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

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
// SpringApplication.run(DemoApplication.class, args);
SpringApplication springApplication = new SpringApplication(DemoApplication.class);
springApplication.setBannerMode(Banner.Mode.OFF);
springApplication.run(args);

}

}