1 环境准备
保证安装完成的软件如下:
JDK 1.8.0_201 (及以上版本)
Apache Maven 3.5.4
IntelliJ IDEA Ultimate 旗舰版 2022.3
2 初始化设置
2.1 认识 IDEA 欢迎页
2.2 初始化 Maven 设置
单击【All settings...】→【Build,Execution,Deployment】→【Build Tools】→【Maven】进入设置Maven界面。
2.3 新建 Spring Boot 项目
方式一:使用 Maven 创建 Spring Boot 项目
搭建步骤:
- 创建 Maven 项目。
- 在pom.xml中添加 Spring Boot 相关依赖。
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>chapter01</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 引入Spring Boot 依赖
1.统一进行版本控制
2.让当前项目具有springBoot特性
3.加载指定的配置文件
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
</parent>
<dependencies>
<!-- 引入Web 场景依赖启动器:引入SpringMVC 的相关依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
- 编写主程序启动类。
在src/main/java路径下创建com/itheima/Application.class。
在类上添加注解,使之变成启动类。
avgpackage com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // 标注该类为主程序启动类
public class Application {
public static void main(String[] args) {
// 在run方法中加载主程序启动类
SpringApplication.run(Application.class);
}
}
4. 创建一个用于Web访问的Controller 。
在itheima路径下创建包controller以及HelloController.java
HelloController.java中的代码。
package com.itheima.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController //该注解为组合注解:@ResponseBody + @Controller
public class HelloController {
// 如果想要访问到HelloController中的hello方法,必须对外暴露一个地址
@GetMapping("/hello") // GetMapping相当于@RequestMapping(value = "/hello",method = RequestMethod.GET)
// 创建一个方法,像页面响应一些数据
public String hello(){
return "Hello World";
}
}
5. 运行项目。
运行Application.java,启动项目成功后,在浏览器中输入网址:
http://localhost:8080/hello,出现“Hello World”说明运行成功。
方式二 :使用 Spring Initializr 方式构建 Spring Boot 项目
- 创建 Spring Boot 项目。
目录结构介绍。
- 创建一个用于 Web 访问的 Controller
在itheima路径下创建包controller以及HelloController.java
HelloController.java中的代码。
package com.ieheima.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "Hello World";
}
}
- 运行项目。
运行ChapterSiApplication.java,启动项目成功后,在浏览器中输入网址:
http://localhost:8080/hello,出现“Hello World”说明运行成功。
报错:
1.错误的类文件: /E:/bigData/apache-maven-3.5.4/repository/org/springframework/boot/spring-boot/3.2.7/spring-boot-3.2.7.jar!/org/springframework/boot/SpringApplication.class 类文件具有错误的版本 61.0, 应为 52.0
2.Plugin 'org.springframework.boot:spring-boot-maven-plugin:not found
3.下面代码中groupId和artifactId飘红提示:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Inspection 'Maven Model Inspection'has no quick-fixes for this problem.Click to edit inspection options,suppress the warning,or disable the inspection completely.
解决:将Spring Boot版本降低为3.0以下,本文使用的Spring Boot版本是2.1.7.RELEASE。
本文暂时没有评论,来添加一个吧(●'◡'●)