简单、快速、便捷。java
搭建普通Spring Web项目项目的通常流程:git
.......github
一系列的配置过程繁杂,很容易遗漏掉,即使是随便搭建一个简单的爬取某个页面的邮箱存入数据库的小项目,都要从头至尾的将配置流程走一遍。web
若是使用Spring Boot,配置则简化了不少,只须要引入响应的几个maven依赖,进行简单的几个配置就能够快速方便的搭建一个Web项目。spring
Dependencies处可经过添加相应的依赖,在生成项目文件时就会自动添加,好比,若是咱们要作Web开发,只要在依赖里添加Web便可。数据库
File -> New -> Spring Starter Project新建项目json
配置项目相关基本信息 浏览器
选择依赖,好比Spring Web tomcat
Finish OK,Spring Boot项目构建完成。bash
Tips:
导入或者Myeclipse构建项目有可能会出现pom.xml文件首行出现错误:Unknown pom.xml /demo line 1 Maven Configuration Problem。
解决方案:
在pom文件中的节点中加入<maven-jar-plugin.version>3.0.0</maven-jar-plugin.version>而后右键项目进入Maven>Update Project...菜单点击,便可
src/main/java
程序开发以及主程序入口:Applicationsrc/main/resources
配置文件存放位置:application.propertiessrc/test/java
测试入口:ApplicationTests生成的Application
和ApplicationTests
类均可以直接运行来启动当前建立的项目,因为目前该项目未配合任何数据访问或Web模块,程序会在加载完Spring以后结束运行。
spring-boot-starter-parent指定了当前项目为一个Spring Boot项目,它提供了诸多的默认Maven依赖。
Spring Boot提供了许多开箱即用的依赖模块,这些模块都是以spring-boot-starter-XX命名的。好比要开启Spring Boot的web功能,只须要在pom.xml中配置spring-boot-starter-web便可:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
复制代码
由于其依赖于spring-boot-starter-parent,因此这里能够不用配置version。
pom.xml 文件中默认有两个模块:
spring-boot-starter
:核心模块,包括自动配置支持、日志和 YAML,若是引入了 spring-boot-starter-web
web 模块能够去掉此配置,由于 spring-boot-starter-web
自动依赖了 spring-boot-starter
。spring-boot-starter-test
:测试模块,包括 JUnit、Hamcrest、Mockito。package com.w3cjava.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@RequestMapping("/hello")
public String index() {
return "Hello World";
}
}
复制代码
@RestController
至关于Spring中的@Controller和@ResponseBody组合使用的,直接以 json 格式输出。
<!-- 支持web的模块依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除tomcat依赖 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
复制代码
<!-- jetty依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
复制代码
模拟对 http://localhost:8080/hello 发送请求测试
package com.w3cjava;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.w3cjava.controller.HelloWorldController;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
private MockMvc mvc;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build();
}
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello World")));
}
}
复制代码
<!-- 热部署依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
复制代码
@RequestMapping("/hello1")
public String index1() {
return "Hello World1";
}
复制代码
spring-boot-maven-plugin:可以以Maven的方式为应用提供Spring Boot的支持,即为Spring Boot应用提供了执行Maven操做的可能。
Spring Boot Maven plugin的5个Goals
整体上而言,经过Spring Boot能够快速构建项目,若是须要使用某个特定的功能,只要添加对应的依赖及简单配置项便可。
欢迎扫面下列二维码关注“余弦的自留地”公众微信号