Springboot项目搭建及踩坑
1. 创建项目
![](https://img2023.cnblogs.com/blog/2935791/202304/2935791-20230411155308144-1538527053.png)
注意:加载start.spring.io
网址与网络有关,如果网速比较慢的话将无法加载成功,可以考虑换成阿里(比较旧):https://start.aliyun.com
![](https://img2023.cnblogs.com/blog/2935791/202304/2935791-20230411155442474-446385945.png)
注意:springboot版本如果3.0以上的,将不支持jdk1.8
如果是新建project的话一定要记得将项目的maven路径设置为本地的maven路径,否则后面加载依赖会非常慢:
![](https://img2023.cnblogs.com/blog/2935791/202304/2935791-20230411160552866-651210796.png)
![](https://img2023.cnblogs.com/blog/2935791/202304/2935791-20230411160711375-581570327.png)
如果遇到打开pom.xml文件后会飘红且项目目录都是灰色(表示为普通目录):
![](https://img2023.cnblogs.com/blog/2935791/202304/2935791-20230411160839908-108208559.png)
右击pom.xml
文件选择 Add As Maven Project
,重新加载,中间可能会卡,等一会儿就好了
![](https://img2023.cnblogs.com/blog/2935791/202304/2935791-20230411163325211-1307369727.png)
2.运行测试
新建一个controller
// TestController.java
package com.junlin.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description:
* @author: junlin623
* @create: 2023-04-11 16:34
*/
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/{id}")
public String test(@PathVariable Integer id) {
System.out.println("id => " + id);
return "<h1>Hello World</h1>";
}
}
![](https://img2023.cnblogs.com/blog/2935791/202304/2935791-20230411163617670-1370399712.png)
直接运行:
![](https://img2023.cnblogs.com/blog/2935791/202304/2935791-20230411163719725-1169502646.png)
又可能会报下图所示的错误(原因就是jdk版本和我创建项目时选择的springboot版本不适配),解决方法就是到pom.xml
文件中修改相应的springboot的版本,修改完记得刷新一下maven::
![](https://img2023.cnblogs.com/blog/2935791/202304/2935791-20230411163755956-2032449113.png)
访问 http://localhost:8080/test/1/
![](https://img2023.cnblogs.com/blog/2935791/202304/2935791-20230411163836903-278796026.png)
成功啦~~~
你只管出发,旅途自有风景~~