搭建SpringBoot项目有几种方式,最简单的是在线创建项目:
先登录网址https://start.spring.io/
然后按照下面步骤操作:
- 选择项目类型,这里默认是Maven选择语言,默认Java选择版本,默认2.0.5设置包名设置项目名搜索需要的依赖库,这里导入了Web库点击创建工程,会下载打包好的项目到本机通过Eclipse或IDEA打开工程
除了在线创建项目以外,IDEA也集成了SpringBoot插件,通过IDEA也可以快速搭建SpringBoot项目。
- 新建项目,选择Spring Initializr ,URL选择Default,下一步
2.设置项目信息,下一步
3.添加依赖,这里只添加了Web
4.设置项目名称和位置,Finish
5.这样一个SpringBoot项目就创建好了,我们看看项目的结构:
DemoApplication 有main方法的类,作用是启动SpringBoot
内容是:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
resources 资源文件目录
static 静态文件目录
templates 模板文件目录
application.properties SpringBoot的配置文件
DemoApplicationTests 测试类
下面测试SpringBoot:
添加一个Controller类:
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/hello")
@ResponseBody
public String hello(Model model){
return "Hello SpringBoot!";
}
}
6.启动springboot
执行DemoApplication的main方法,SpringBoot会启动自带的tomcat,会看到下面图形,如果没有出现错误信息,就启动成功了。
7.在浏览器输入Controller的URL: