本文源码: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)发展趋势看

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

2、一张图形容学SpringBoot的心情

img

二、搭建SpringBoot的环境

1、创建一个Maven项目

2、引入核心依赖

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

3、编写配置文件

application.yml

# 端口
server:
port: 8001

4、启动文件注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public 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 第一个程序
*/
@RestController
public 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)参数内容读取

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

4)调用方式

/**
* 环境配置,参数绑定
*/
@RestController
public 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)
@WebAppConfiguration
public 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-base
GitEE·地址
https://gitee.com/cicadasmile/spring-boot-base

SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口的更多相关文章

  1. SpringBoot2.0 基础案例(12):基于转账案例,演示事务管理操作

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.事务管理简介 1.事务基本概念 一组业务操作ABCD,要么全部 ...

  2. SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.文件上传 文件上传是项目开发中一个很常用的功能,常见的如头像上 ...

  3. SpringBoot2.0 基础案例(08):集成Redis数据库,实现缓存管理

    一.Redis简介 Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, Elastic ...

  4. SpringBoot2.0 基础案例(16):配置Actuator组件,实现系统监控

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Actuator简介 1.监控组件作用 在生产环境中,需要实时 ...

  5. SpringBoot2.0 基础案例(15):配置MongoDB数据库,实现增删改查逻辑

    本文源码:GitHub·点这里 || GitEE·点这里 一.NoSQL简介 1.NoSQL 概念 NoSQL( Not Only SQL ),意即"不仅仅是SQL".对不同于传统 ...

  6. SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Cache缓存简介 从Spring3开始定义Cache和Cac ...

  7. SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件

    一.Mybatis框架 1.mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获 ...

  8. SpringBoot2.0 基础案例(09):集成JPA持久层框架,简化数据库操作

    一.JAP框架简介 JPA(Java Persistence API)意即Java持久化API,是Sun官方在JDK5.0后提出的Java持久化规范.主要是为了简化持久层开发以及整合ORM技术,结束H ...

  9. SpringBoot2.0 基础案例(07):集成Druid连接池,配置监控界面

    一.Druid连接池 1.druid简介 Druid连接池是阿里巴巴开源的数据库连接池项目.Druid连接池为监控而生,内置强大的监控功能,监控特性不影响性能.功能强大,能防SQL注入,内置Login ...

随机推荐

  1. 开源流媒体播放器EasyPlayer

    配套开源流媒体服务器EasyDarwin,我们开发了一款开源的流媒体播放器EasyPlayer(现在已经升级合并到开源EasyClient中):同样,EasyPlayer目前只支持RTSP流媒体协议, ...

  2. go map 线程不安全 安全措施

    go map   线程不安全  安全措施

  3. office web apps的搭建部署(1)(写于2017.12.27)

    因为业务方面的需求,项目要求搭建office-web-apps这个玩意儿,做一个在线预览编辑的功能,为了方便,我下面都用OWA代替这个服务. 首先说一下什么是office-web-apps-serve ...

  4. Linux随笔-鸟哥Linux服务器篇学习总结(全)

    作者:Danbo 时间:2015-7-17 在runlevel3启动级别下默认启动网络挂载(autofs)机制,我们可以通过命令将其关闭:chkconfig autofs off 或者 /etc/in ...

  5. dhcpcd守护进程分析【转】

    本文转载自;http://blog.csdn.net/lishanmin11/article/details/37930073 最近在调android ethernet功能,android本身不带 e ...

  6. C++中map容器的说明和使用技巧

    C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值. 一.map的说明 1 头文件 #include <map> 2 定义 ma ...

  7. v-for指令用法一

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  8. Asterisk 通话过程中执行动作(即applicationmap )的使用方法和电话转会议的实现

      asterisk在正常通话过程中执行拨号计划中动作是通过feature.conf中的[applicationmap ]下定义的,举例如下: nway-start => *0,callee,M ...

  9. SPOJ MAXOR (分块 || 可持久化字典树 || 异或)(好题)

    You are given a sequence A[1], A[2], ..., A[N]. (0 ≤ A[i] < 231, 1 ≤ N ≤ 12000). A query is defin ...

  10. rsync(四)技术报告

    1.1 摘要 本报告介绍了一种将一台机器上的文件更新到和另一台机器上的文件保持一致的算法.我们假定两台机器之间通过低带宽.高延迟的双向链路进行通信.该算法计算出源文件中和目标文件中一致的部分(译者注: ...