当前位置:   article > 正文

使用Spring Initializr创建Spring Boot工程_idea2020创建spring initializer

idea2020创建spring initializer

目录

1、开发环境

2、创建Spring Initializr工程

3、Spring Boot父工程

4、Web场景依赖

5、添加Controller

6、添加配置文件

7、运行服务

8、浏览器访问服务


1、开发环境

  • IDE:IntelliJ IDEA 2020.3.4
  • JDK:1.8
  • Maven:3.5+
  • Spring Boot:2.6.7

2、创建Spring Initializr工程

  • 打开IDEA,点击【New Project】按钮

  • 选择【Spring Initializr】工程,选择SDK版本为【1.8】,并选择创建项目使用的URL,默认使用官方提供的URL,最后点击【Next】
    • 由于默认的spring官网的地址在国内比较难访问,当访问不了的时候可以使用阿里云的地址:https://start.aliyun.com/,但是创建的项目使用的spring boot的版本可能比较低

  • 填写项目坐标信息、包路径等,点击【Next】按钮

  • 选择【Web】开发场景,点击【Next】按钮

  • 填写项目名称、位置等信息,点击【Finish】按钮

  • 最后生成标准的Spring Boot工程,默认自动生成以下内容:
    • 主程序:SpringBootTestApplication
    • 配置文件:application.properties
    • 测试类:SpringBootTestApplicationTests

  • 可以删除其他不需要的文件

3、Spring Boot父工程

  • 使用Spring Initializr创建Spring Boot工程时,会在pom.xml文件自动添加其父工程:spring-boot-starter-parent

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.6.7</version>
  5. <relativePath/>
  6. </parent>

4、Web场景依赖

  • 需要检查一下是否有自动添加web场景依赖,一般来说,在创建的过程中勾选了Web场景,Spring Initializr会自动添加Web场景依赖,如果没有,需要手动添加该依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>

5、添加Controller

  1. @RestController
  2. public class HelloController {
  3. @GetMapping("/hello")
  4. public String hello() {
  5. return "Hello, Spring Boot!";
  6. }
  7. }

6、添加配置文件

  • 虽然已经有了application.properties配置文件,但是在项目中使用yaml配置文件较多,可删除已有的配置文件,然后重新添加yaml配置文件
  • 配置文件名:application.yml
  • 指定端口名:8888
  1. server:
  2. port: 8888

7、运行服务

8、浏览器访问服务

访问地址:http://127.0.0.1:8888/hello

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/921003
推荐阅读
相关标签
  

闽ICP备14008679号