Maven构建SpringBoot项目

目的

构建一个Spring Boot项目,并实现一个简单的Http请求处理
了解Spring Boot项目创建,运行过程,项目结构,简单、开发快速的特性

Spring boot的优点

1,开发者快速入门
2,开箱即用(自带各种默认配置,简化项目配置项)
3,无冗余代码生成和XML配置文件

创建SpringBoot项目

访问:http://start.spring.io/ , 通过SPRING INITIALIZR工具生成基础项目

Maven构建SpringBoot项目

查看项目

使用idea,导入Maven项目,项目结构如下:
Maven构建SpringBoot项目

引入Web模块

新建工程的pom.xml,引入了2个依赖模块
spring-boot-starter:核心模块,包括自动配置支持、日志和YAML
spring-boot-starter-test:测试模块,包括JUnit、Hamcrest、Mockito

需要引入Web模块 : pring-boot-starter-web

<dependencies>

   <!-- 核心模块,包括自动配置支持、日志和YAML -->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
   </dependency>

   <!-- 测试模块,包括JUnit、Hamcrest、Mockito -->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
   </dependency>

   <!-- 引入Web模块 -->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>`
</dependencies>

添加spring-boot-starter-web配置后,运行maven->Reimport,下载模块文件
Maven构建SpringBoot项目
添加spring-boot-starter-web后,项目支持web注解,如

@RestController,
@RequestMapping("/hello")

添加HelloWord服务

Maven构建SpringBoot项目

启动主程序

Maven构建SpringBoot项目
打开浏览器访问http://localhost:8080/hello,页面输出Hello World
Maven构建SpringBoot项目
Web项目创建完成