一:SpringBoot是什么
springboot是对spring的缺点进行改善和优化,约定大于配置 开箱即用 没有代码生成 也无需xml 文件配置 能够修改属性值来知足需求java
1) Spring Boot使编码变简单web
2) Spring Boot使配置变简单spring
3) Spring Boot使部署变简单apache
4) Spring Boot使监控变简单浏览器
二:建立第一个SpringBoot工程
一、点击File--->New--->Project...

二、输入MAVEN,组名、包名等相关参数

三、选择SpringBoot版本,选择项目须要依赖的相关骨架包
注意:有些版本此处显示的是SpringWeb是同样的springboot

四、设置项目保存目录:

五、项目建立完成,工程主界面以下:
删除多余的这三个文件app

六、项目说明
(1)、默认有个Demo001Application类,里面是spring boot的载入函数maven
(2)、resource目录下有个application.properties文件,这个是Spring boot的配置文件函数
(3)、test目录下有个测试类Demo001ApplicationTests,这个是spring boot的单元测试spring-boot
(4)、pom.xml文件
<?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> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.offcn</groupId> <artifactId>firstdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>firstdemo</name> <description>Demo project for Spring Boot</description>
<properties> <java.version>1.8</java.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.6</version> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project> </project> |
注意观察
一个继承spring-boot-starter-parent,两个依赖,spring-boot-starter-web web项目依赖必须,spring-boot-starter-test spring boot项目单元测试依赖
注意,不少人配置的maven远程仓库地址,其中不少配置的阿里云maven镜像,在这会有找不到最新Springboot相关包的问题,请把远程仓库指向华为云:
<mirror> <id>huaweicloud</id> <mirrorOf>*</mirrorOf> <url>https://mirrors.huaweicloud.com/repository/maven/</url> </mirror> |
六、启动项目
找到以下文字,代表SpringBoot已经成功启动:

打开浏览器,输入地址:http://localhost:8080 ,出现以下画面

出现上图404错误是正常的,由于咱们什么都没写。
七、编写HelloController
package com.offcn.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @RequestMapping(value="/hello",method=RequestMethod.GET) @ResponseBody public String sayHello() { return "hello spring Boot!"; } } |
注意HelloController所在包,必须在com.offcn.demo包,或者子包下面。
重启发现刚才写的hello已经映射出来了
访问http://localhost:8080/hello
