本文源码:GitHub·点这里 || GitEE·点这里

一、SpringBoot 框架的特点1、SpringBoot2.0 特点

1)SpringBoot继承了Spring优秀的基因,上手难度小
2)简化配置,提供各种默认配置来简化项目配置
3)内嵌式容器简化Web项目,简化编码
Spring Boot 则会帮助开发着快速启动一个 web 容器,在 Spring Boot 中,只需要在 pom 文件中添加如下一个 starter-web 依赖即可.

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>

4)发展趋势看
微服务是未来发展的趋势,项目会从传统架构慢慢转向微服务架构,因为微服务可以使不同的团队专注于更小范围的工作职责、使用独立的技术、更安全更频繁地部署。二、搭建SpringBoot的环境1、创建一个Maven项目

2、引入核心依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>3、编写配置文件

application.yml

# 端口server: port: 80014、启动文件注解

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class,args) ; }}

丝毫没有问题,就这样吧启动上面这个类,springboot的基础环境就搭建好了。
想想之前的Spring框架的环境搭建,是不是就是这个感觉:意会一下吧。三、SpringBoot2.0 几个入门案例1、创建一个Web接口

import com.boot.hello.entity.ProjectInfo;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * SpringBoot 2.0 第一个程序 */@RestControllerpublic class HelloController { @RequestMapping("/getInfo") public ProjectInfo getInfo (){ ProjectInfo info = new ProjectInfo() ; info.setTitle("SpringBoot 2.0 基础教程"); info.setDate("2019-06-05"); info.setAuthor("知了一笑"); return info ; }}

@RestController 注解 等价 @Controller + @ResponseBody 返回Json格式数据。

2、参数映射

1)首先看看SpringBoot 如何区分环境

这里标识配置加载指定的配置文件。

2)参数配置
application-pro.yml

user: author: 知了一笑 title: SpringBoot 2.0 程序开发 time: 2019-07-05

3)参数内容读取

@Componentpublic class ParamConfig { @Value("${user.author}") private String author ; @Value("${user.title}") private String title ; @Value("${user.time}") private String time ; // 省略 get 和 set 方法}

4)调用方式

/** * 环境配置,参数绑定 */@RestControllerpublic class ParamController { @Resource private ParamConfig paramConfig ; @RequestMapping("/getParam") public String getParam (){ return "["+paramConfig.getAuthor()+";"+ paramConfig.getTitle()+";"+ paramConfig.getTime()+"]" ; }}3、RestFul 风格接口和测试

1)Rest风格接口

/** * Rest 风格接口测试 */@RestController // 等价 @Controller + @ResponseBody 返回Json格式数据@RequestMapping("rest")public class RestApiController { private static final Logger LOG = LoggerFactory.getLogger(RestApiController.class) ; /** * 保存 */ @RequestMapping(value = "/insert",method = RequestMethod.POST) public String insert (UserInfo userInfo){ LOG.info("===>>"+userInfo); return "success" ; } /** * 查询 */ @RequestMapping(value = "/select/{id}",method = RequestMethod.GET) public String select (@PathVariable Integer id){ LOG.info("===>>"+id); return "success" ; }}

2)测试代码

@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = MockServletContext.class)@WebAppConfigurationpublic class TestRestApi { private MockMvc mvc; @Before public void setUp() throws Exception { mvc = MockMvcBuilders.standaloneSetup(new RestApiController()).build(); } /** * 测试保存接口 */ @Test public void testInsert () throws Exception { RequestBuilder request = null; request = post("/rest/insert/") .param("id", "1") .param("name", "测试大师") .param("age", "20"); mvc.perform(request) .andExpect(content().string(equalTo("success"))); } /** * 测试查询接口 */ @Test public void testSelect () throws Exception { RequestBuilder request = null; request = get("/rest/select/1"); mvc.perform(request) .andExpect(content().string(equalTo("success"))); }}

这样SpringBoot2.0的入门案例就结束了,简单,优雅,有格调。

四、源代码

GitHub·地址https://github.com/cicadasmile/spring-boot-baseGitEE·地址https://gitee.com/cicadasmile/spring-boot-base