使用 IDEA 来搭建一个 SpringBoot 的项目非常简单
1 点击 Create New Project,选择 Spring Initializr
2 点击 Next,输入包名
3 点击 Next,选择需要的插件
没有选择也没有关系,这些插件在后面也是可以在 pom 文件中添加的,这里为了快速运行只选择了 Spring Web Starter,需要数据库的插件的话可以根据自己需要添加。
4 点击 Next,输入项目名,路径
5 点击 Finish,等待构建完成
构建完成后基本结构结构如下
其中重要的几个文件如 DemoApplication 是整个程序的入口类,application.properties 是项目的配置文件,pom.xml 是项目的依赖。
6 添加 HelloController
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* Description:测试 Controller
* Author: QinHao
* Date: 2019/7/25 13:45
*/
@RestController
@RequestMapping(value = "/")
public class HelloController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public Map<String, String> helloWorld() {
System.out.println("Hello World");
Map<String, String> map = new HashMap<>();
map.put("data", "Hello World");
return map;
}
}
7 运行项目
控制台打印如下内容则说明成功启动:
然后在在浏览器输入 localhost:8080,会显示如下内容:
控制台也能看到成功调用了 helloWorld 方法:
至此 SpringBoot 项目构建完毕,成功运行。