【SpringBoot1】创建第一个SpringBoot项目 末蓝、 2024-03-29 14:56 17阅读 0赞 创建SpringBoot项目可以通过两种方式: 1、通过访问:https://start.spring.io/,SpringBoot的官方网站进行创建SpringBoot项目; 2、通过工具(例如:Idea)创建SpringBoot项目。本次使用IDEA创建第一个SpringBoot项目。 首先,打开我们的Idea开发工具 ![d150b0d9d33349daa3b1194b42c4969c.png][] 我们使用Spring Initializr创建SpringBoot项目,SDK默认选择你安装的JDK路径,默认选择SpringBoot官网地址,点击Next![18caae32c5604cb9911cd274a10f5675.png][] 在这里填写你的包名和项目名,我们使用Maven Project,选择项目打包方式为:jar,Java版本为8,默认就好,点击Next ![cd186cbf10eb42c3b300f8cea091dea0.png][] 然后选择我们需要使用到的Maven依赖,这里我只选择了Web项目开发的依赖,当然,你也可以选择更多的依赖,例如:MySQL,jpa,lombok,还有SpringCloud的一些东西,根据自己需要来选择,点击Next ![1a1840da9ea64a09bbc2ee88d61e10ae.png][] 这里是让你填写你的项目名称和项目地址的信息,点击Finish,我们就完成了第一个SpringBoot项目的创建工作,接下来只需要等待Idea成功引入Maven依赖,项目就算创建完成![727adfbb8af24c43bce90bda10962f99.png][] 我们的SpringBoot项目创建完成之后的目录结构就是这个样子,.mvn,mvnw,mvnw.cmd这三个文件是可以删除的,DemoApplication.java是我们这个项目的启动文件,我们在启动项目的时候,只需要Run这个文件或者Debug Run这个文件就可以了,接下来我们看看pom.xml文件的内容 ![9a1ec5fd300a46ccbb3370474eabe61c.png][] <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> * 我们在选择依赖的时候,选择了web的依赖,已经成功引入,另外还默认引入了测试的依赖,接下来就让我们来启动它吧 * package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @RequestMapping(value = "/init") public String init(@RequestParam("name") String name) { return "Hello," + name + "!"; } } 在启动之前,我们在项目的启动文件上加了@RestController注解,它是@Controller和@ResponseBody的结合,写上@RestController注解,就相当于把后两者都给写上了,并写了一个地址为“init”方法,接收一个“name”参数,返回一个String类型的数据,然后我们启动项目 ![8cedd92dc1574d5a9abeb16669e0f8af.png][] 我们启动后会看到SpringBoot的字样,然后在启动信息中会看到启动在本地的8080端口,以及启动费时等信息,然后我们打开浏览器访问地址:http://localhost:8080/init?name=World 页面显示信息:Hello,World!,代表我们的项目启动成功,并能够成功访问! ![61b057f0e42d4c36ac1e2e35ea025a88.png][] [d150b0d9d33349daa3b1194b42c4969c.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/28/4fa791d347ed4eb9b471b3277aadecd0.png [18caae32c5604cb9911cd274a10f5675.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/28/97c235ff565745f99c1f111bdb867751.png [cd186cbf10eb42c3b300f8cea091dea0.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/28/b43ceb9cfb814b15a4ce69dc660a68e7.png [1a1840da9ea64a09bbc2ee88d61e10ae.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/28/e1f153b18a5649e5be8a5fc4a5189db3.png [727adfbb8af24c43bce90bda10962f99.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/28/6658bf7b448e448fabda10d69f91c169.png [9a1ec5fd300a46ccbb3370474eabe61c.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/28/e84627119ecb42ddaa9fd10c0c50e843.png [8cedd92dc1574d5a9abeb16669e0f8af.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/28/217374a699374db783f79c3d5ba6cf85.png [61b057f0e42d4c36ac1e2e35ea025a88.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/28/d62e6505debe4742b08e0c6475336029.png
还没有评论,来说两句吧...