# Spring boot入门

# 应具备条件

  1. 熟悉spring家族
  2. 熟悉java-web开发
  3. 对maven有一定的基础。

# 使用工具

这里所使用的均为Eclipse进行开发。IDEA的请自行摸索。

# 入门示例

# 创建项目

使用eclipse右键new > mavenProject

所有配置默认即可。

# 编辑pom.xml文件

pom.xml加入spring-boot依赖包

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.qm</groupId>
	<artifactId>spring-boot-helloWord</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>spring-boot-helloWord</name>
	<url>http://maven.apache.org</url>

	<!-- Spring boot父节点
	配置后spring boot会自动对其他的依赖包进行版本最佳配置
	作用:无需再指定版本、自动进行最佳版本配置 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.1.RELEASE</version>
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<!-- 制定Java的JDK版本,默认是1.6 -->
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!-- junit测试包 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- spring-boot web依赖,集成了MVC的相关依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
</project>

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

# 创建Controller

HelloController

@RestController
public class HelloController {
	
	@RequestMapping("/hello")
	public String hello() {
		return "helloWord";
	}
}

1
2
3
4
5
6
7
8
9

# 创建Spring-boot启动类

@SpringBootApplication
public class App {
	
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}
1
2
3
4
5
6
7

# 运行

  1. 运行main方法,控制台会打印出spring-boot的日志信息,并发现已经自动启动了tomcat,端口为8080

  2. 访问刚才编写的controllerhello接口

http://localhost:8080/hello
1
  1. 第一个Spring-boot的web项目完成。浏览器显示helloWord
最近更新: 2019/10/17 上午4:20:42