博主信息
博文 41
粉丝 0
评论 0
访问量 40803
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
2、【转载】IDEA创建Spring Boot项目
自由之上
原创
588人浏览过

1、创建 Spring Boot 项目

Intellij IDEA 一般可以通过两种方式创建 Spring Boot 项目:

  • 使用 Maven 创建
  • 使用 Spring Initializr 创建

1、使用 Maven 创建

1. 使用 IntelliJ IDEA 创建一个名称为 helloworld 的 Maven 项目

项目目录

  1. 在该 Maven 项目的 pom.xml 中添加以下配置,导入 Spring Boot 相关的依赖。
  1. <project>
  2. ...
  3. <parent>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-parent</artifactId>
  6. <version>2.4.5</version>
  7. <relativePath></relativePath> <!-- lookup parent from repository -->
  8. </parent>
  9. <dependencies>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-web</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-test</artifactId>
  17. <scope>test</scope>
  18. </dependency>
  19. </dependencies>
  20. ...
  21. </project>

更新pom
2. 在com.study.start包下,创建一个名为 MyApplication 主程序,用来启动 Spring Boot 应用,代码如下。

  1. package com.study.start;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class MyApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(MyApplication.class, args);
  8. }
  9. }

2、使用 Spring Initializr 创建


选择 Spring Boot 的版本及所依赖的 Spring Boot 组件

2、启动 Spring Boot

直接运行启动类 Helloworld2Application 中的 main() 方法,便可以启动该项目

3、添加接口演示

\helloworld2\src\main\java\com\example\helloworld2\controller\HelloController.java

  1. package com.example.helloworld2.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.ResponseBody;
  5. @Controller
  6. public class HelloController {
  7. @ResponseBody
  8. @RequestMapping("/hello")
  9. public String hello() {
  10. return "Hello World!";
  11. }
  12. }

访问接口地址:
http://localhost:8080/hello

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学