1.通过IDEA的spring Initializer创建(需要连网)
![72491a9721c12f35d8cf113f417724d2.png](https://i-blog.csdnimg.cn/blog_migrate/3e7b74f5cc8c4bc8f834b69a274ee868.jpeg)
如图操作,点击Next
![7730c86565bdece7ccc387a2dd74c697.png](https://i-blog.csdnimg.cn/blog_migrate/2c7f21d52b0d76c505e3c9ea5ca18869.jpeg)
如图操作,点击Next
![1ba9cad39c4992498ff64e002a882d1a.png](https://i-blog.csdnimg.cn/blog_migrate/3299b160fba39d61e9db7c39fa63486d.jpeg)
如图操作,点击Next
![3f147ced9b6b26794775dacea4d841a3.png](https://i-blog.csdnimg.cn/blog_migrate/c26d6eed46cc325711e56a9122b7c708.jpeg)
如图操作,点击Finish,完成创建。
2.目录结构介绍
![33e826eebc783fd7dfec92082a7d8064.png](https://i-blog.csdnimg.cn/blog_migrate/7bc9aefcad553ebf7a58f942069ac1d6.jpeg)
创建之后默认的目录结构,如图所示:
Springboot需要的目录及文件都已经被创建出来,在此基础上进行 开发即可。
3.编写controller层测试类
在com.test目录下创建包:controller;然后在此包下创建类:TestController.java,代码如下:
package com.test.controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;@RestControllerpublic class TestController { @RequestMapping("/index") public String index(Model model, HttpServletResponse response) { return "hello spring boot index"; }}
注意:@RestController 注释表示发布的是rest接口
4.启动程序
在自动生成的主类 Demo2Application.java 文件上点击右键,然后如下图操作启动程序
![21c5a3a221a9b338bfa36389933eec92.png](https://i-blog.csdnimg.cn/blog_migrate/4c6a0618611ac1791d4af680767b678b.jpeg)
出现下图框表示启动成功
![f08ca9825b929bcca6f37003ce33128b.png](https://i-blog.csdnimg.cn/blog_migrate/f17b63cb38fc04572c0f6b49ec0a1872.jpeg)
5.浏览器测试接口
打开浏览器
![44613e555ce6559074152f39ee5c271b.png](https://i-blog.csdnimg.cn/blog_migrate/bbcfce8db26c0f920646a3473c1f16eb.jpeg)
输入:http://localhost:8080/index ,出现如下界面,说明接口可以正常访问
总结:此种方法创建的工程,是官方推荐的方法,目录和默认的文件都已经给配置完好,直接使用即可,缺点是需要联网!!!
相比利用Maven archetype原型创建方法要方便的多,因此推荐使用此方法!!!
本文由原创作者多平台发布,csdn平台地址:https://blog.csdn.net/houpeibin2012/article/details/104220043