Spring Boot 入门学习指南( 三 )


在类的上面添加@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 路径,我写了结构更加清晰,知识点更加系统的课程 。




推荐阅读