0
点赞
收藏
分享

微信扫一扫

Idea创建SpringBoot工程(两种方法)

星巢文化 2022-01-22 阅读 124

方式一

(1)创建maven工程

创建一个maven工程(建议:java工程,也可以是web工程),无需勾选maven骨架

1、添加起步依赖(依赖一个父工程)

2、添加web依赖

<!--父工程-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
</parent>

<!--起步依赖web-->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

(2)工程jar依赖情况

此时我们可以发现工程自动引入了很多包,其中还包含tomcat包。

(3)引导程序

该程序是发布springboot应用的入口(只需要run一下)

 @SpringBootApplication
 public class DemoApplication {
     public static void main(String[] args) {
         // 只需要run一下,就能发布一个springboot应用
         // 相当于之前将web工程发布到tomcat服务器,只是在springboot中集成了tomcat插件
         SpringApplication.run(DemoApplication.class,args);
     }
 }

(4) 编写HelloController

在工程的src目录下创建com.qf.controller.HelloController

 @RestController
 public class HelloController {
     /**
      * 请求 /hello  输出hello springboot!
      * @return
      */
     @RequestMapping(value = "/hello")
     public String hello(){
         return "hello springboot!";
     }
 }

(5) 测试

在DemoApplication里运行主方法,访问:http://localhost:8080/hello

方式二(建议)

(1)创建spring initializr工程

通过idea工具创建工程时,不再选择maven了而是选择spring initializr。然后去勾选相关依赖。

  • step1:新建module,选择spring initializr,然后下一步(如果默认网址不行可以换https://start.aliyun.com/)

 

 step2:填写项目相关信息

 step3:勾选需要的依赖

 step4:完成,编写HelloController

@RestController
public class HelloController {
    /**
     * 请求 /hello  输出hello springboot!
     * @return
     */
    @RequestMapping(value = "/hello")
    public String hello(){
        return "hello springboot! demo2!";
    }
}

 运行测试即可。

举报

相关推荐

0 条评论