一, 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.YAML
- # 端口
- 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.YAML
- 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(SpringJUnit4Cla***unner.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
码云地址: 知了一笑
https://gitee.com/cicadasmile
来源: http://www.bubuko.com/infodetail-3147603.html