创建springboot项目有多种方式
本文就是使用maven创建springboot项目做一个记录
创建maven项目,按下面的选择,然后就一直next
创建项目进来后,修改pom文件(主要添加springboot的父级依赖,以及一些起步依赖包,如starter-web,starter-test,devtools等)
4.0.0 org.lys blog-parent 1.0-SNAPSHOT spring-boot-starter-parent org.springframework.boot 2.6.2 blog-parent http://www.example.com UTF-8 1.7 1.7 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test org.springframework.boot spring-boot-devtools true org.springframework.boot spring-boot-maven-plugin 2.6.3
编写启动类
package com.lys;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
编写一个Controller测试一下
package com.lys;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@RequestMapping("/hello")
public String hello() {
return "hello!!!!";
}
}
启动项目,访问lcoalhost下的/hello
通过以上,搭建起步springboot就好了!!!
【踩坑提示】
package com.lys;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class Test1 {
@Test
void demo() {
System.out.println(2);
}
}
我在启动上面的代码时报下面的错:
经过排查,导致上述的问题的原因是【启动类所在的包和单元测试的包不在同一级根目录下】,如下:
将上面的包名改成一致,即【org】改成【com】,就不会再报错了



