在类的上面添加@SpringBootTest,系统会自动加载 Spring Boot 容器 。在日常测试中,我们就可以注入 bean 来做一些局部业务的测试 。MockMvcRequestBuilders可以 post、get 请求,使用print方法会将请求和相应的过程都打印出来,如下:
MockHttpServletRequest: HTTP Method = POST Request URI = /hello Parameters = {name=[neo]} Headers = {}Handler: Type = com.neo.helloWorld.web.HelloWorldController Method = public java.lang.String com.neo.helloWorld.web.HelloWorldController.hello(java.lang.String)...MockHttpServletResponse: Status = 200
Error message =
Headers = {Content-Type=[text/plain;charset=ISO-8859-1], Content-Length=[16]} Content type = text/plain;
charset=ISO-8859-1
Body = Hello World ,neo Forwarded URL =
Redirected URL =
Cookies =
从返回的Body = Hello World ,neo可以看出请求成功 。
7.总结使用 Spring Boot 可以非常方便、快速搭建项目,而不用关心框架之间的兼容性、适用版本等各种问题,我们想使用任何东西,仅仅添加一个配置就可以,所以使用 Sping Boot 非常适合构建微服务 。
建议大家使用 Idea 开发 Spring Boot 项目,Eclipse 对 Spring Boot 项目支持并不好,并且使用 Eclipse 偶尔会出现一些诡异的问题,影响初学者的学习 。
关于完整的学习 Spring Boot 路径,我写了结构更加清晰,知识点更加系统的课程 。
推荐阅读
- Spring boot 配置类与配置文件详解
- 微服务网关除了zuul、spring cloud gateway还有更出色的
- Java新手如何学习Spring、Struts、Hibernate三大框架?
- 七个开源的 Spring Boot 前后端分离项目,建议收藏加转载
- 3个Spring Boot项目调优的方式
- springboot配置文件切换讲解
- Python网络爬虫:Beautiful Soup库入门
- GitLab CI构建SpringBoot-2.3应用
- 面试官问 Spring AOP 中两种代理模式的区别,我懵逼了
- Spring Boot 上传图片完整示例
