IDEA快速搭建SpringBoot项目并输出hello world

1.首先打开IDEA创建一个新的项目

在这里插入图片描述

2.选择Spring项目

在这里插入图片描述

3.根据提示一步步操作

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这样就创建好了一个SpringBoot项目

4.接下来在网页中输出“hello world”

4.1在\src\test\java\com\example\demo目录下创建一个controller包,在包下新建HelloController类

在这里插入图片描述

4.2 编辑HelloController类
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping("/test")
    public String hello(){
        return "hello world!"; 
    }
}

在这里插入图片描述

4.3 编辑DemoApplicationTests类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplicationTests {
    public static void main(String[] args){
        SpringApplication.run(DemoApplicationTests.class, args);
    }
}

在这里插入图片描述

4.4运行main方法

在这里插入图片描述

4.5 默认端口为8080,在浏览器中输入 http://localhost:8080/test

在这里插入图片描述
注:这里的“/test”取决于HelloController类中 @RequestMapping("/test") 引号里的内容!
切记要创建controller包,并将文件放在正确的目录下!
切记正确的编辑DemoApplicationTests主类!

hello world就运行出来啦!
发布了9 篇原创文章 · 获赞 1 · 访问量 459

猜你喜欢

转载自blog.csdn.net/cMengZ/article/details/104577690