目录
一、博客引言。
(1)SpringBoot3实战学习篇开篇介绍。
(2)Vue3实战学习篇回顾介绍。
二、从0快速搭建SpringBoot3工程。(详细步骤)
(1)项目层次结构说明。
(2)SpringBoot3新工程正式搭建。
<1>新建项目(new project)。
<2>最新且稳定版(JDK21)下载、安装、配置系统环境。
<3>SpringBoot新工程的构建选择项。
<4>SpringBoot新工程构建版本、依赖项选择。
<5>"精简"新工程无用的目录、文件。
<6>"精简"pom.xml文件。
<7>设置项目编码。
<8>配置Maven。
<9>pom文件添加为Maven项目。
(3)启动并运行SpringBoot3工程。
<1>SpringBootApplication。(SpringBoot3工程启动类)
<2>java: 错误: 不支持发行版本 21!(解决)
<3>启动成功。但报错未配置DataSource!
<4>application.yml中配置数据库信息。
<5>application.yml中修改默认端口号。
(4)新建测试接口。(验证SpringBoot程序)
<1>新建controller包。
<2>WebController类。(注解@RestController)
<3>WebController类的hello()方法。(注解@GetMapping)
<4>重新启动工程并测试接口。
404访问资源未找到原因与解决方法。
移动SpringBootApplication启动类。
重新启动工程再次访问localhost:9090/hello。
<5>接口中返回Integer类型的方法。
(5)封装访问接口的结果类。(状态码code、请求信息msg、返回数据data)
<1>新建统一封装结果类。(Result类)
<2>提供对应的getter、setter方法。
<3>配置成功(success)静态方法。(成功、有无数据返回)
请求成功且无数据返回。
请求成功且有数据返回。
修改接口中的方法。使用封装类Result封装结果集。
<4>配置失败(error)静态方法。(失败、异常信息返回)
请求失败且无数据返回。
全局异常处理器代码。(处理所有系统出现的异常!)
手动限制访问路径。(使用自定义异常)
自定义异常类代码。
全局异常处理器类中——>配置自定义异常类。
请求失败且无参数返回。(有参数)
<5>最终的自定义封装结果类(Result)。
一、博客引言。
(1)SpringBoot3实战学习篇开篇介绍。
- 《SpringBoot3实战学习篇》是博主是为了结合前段时间的Vue3实战学习完成从0到1独立开发并快速搭建SpringBoot3+Vue3的实战项目(管理系统)。
(2)Vue3实战学习篇回顾介绍。
- 关于《Vue3实战学习篇》的所有详细教程(Vue3环境搭建、Vue3基础脚手架搭建、Vue3基础语法、Vue3集成Element-Plus、Vue3集成Vue-Router、Vue3快速搭建管理系统)都在博主专栏《Vue实战学习篇》下。
- 《Vue3实战学习篇》专栏链接
二、从0快速搭建SpringBoot3工程。(详细步骤)
(1)项目层次结构说明。
(2)SpringBoot3新工程正式搭建。
<1>新建项目(new project)。
<2>最新且稳定版(JDK21)下载、安装、配置系统环境。
- 因为SpringBoot3工程环境JDK版本最低JDK17+。IDEA版本最好2024+。
- 官网地址:Java Downloads | Oracle
- 配置系统环境变量。
- cmd命令行检测。
java -version
<3>SpringBoot新工程的构建选择项。
<4>SpringBoot新工程构建版本、依赖项选择。
- SpringBoot版本对于新手——版本选择越低越好。(怕出错)
- 依赖SpringWeb——接收前端数据、访问外部接口。
- 依赖MySQL-Driver——MySQL数据库驱动。
- 依赖持久层框架(MyBatis)——数据库的增删改查。
<5>"精简"新工程无用的目录、文件。
- 继续修剪工程无用目录或文件。
- application.properties文件的修改。
<6>"精简"pom.xml文件。
- 删除于本项目学习——无用标签。
- 删去对于本项目学习——无用依赖项。
<7>设置项目编码。
<8>配置Maven。
- 详细关于Maven的下载、安装、环境配置博客链接。Maven详细教程
<9>pom文件添加为Maven项目。
- 解析完成后。依赖项也存在。但依旧"满江红"。重新打开项目即可。
- 完成Maven依赖项的解析、下载和安装。
(3)启动并运行SpringBoot3工程。
<1>SpringBootApplication。(SpringBoot3工程启动类)
- 右击启动运行对应的main()方法即可。
<2>java: 错误: 不支持发行版本 21!(解决)
- 项目结构。
- 启动类编辑配置。
<3>启动成功。但报错未配置DataSource!
<4>application.yml中配置数据库信息。
# 数据库配置(驱动) spring:datasource:#连接到MySQL数据库所需的JDBC驱动(新的)driver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: root123url: jdbc:mysql://localhost:3306/xm-pro?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true
<5>application.yml中修改默认端口号。
- 默认端口号:8080。
#修改Spring Boot应用启动后监听的端口号为9090。 server:port: 9090# 数据库配置(驱动) spring:datasource:#连接到MySQL数据库所需的JDBC驱动(新的)driver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: root123url: jdbc:mysql://localhost:3306/xm-pro?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true
- 重新启动SpringBoot工程。
(4)新建测试接口。(验证SpringBoot程序)
<1>新建controller包。
<2>WebController类。(注解@RestController)
- @RestController是Spring框架里的一个注解。它把一个类标记成RESTful Web服务的控制器。这个注解是@Controller和ResponseBody的组合,意味着控制器里所有方法的返回值都会被直接序列化为HTTP响应体。
package com.hyl.controller;import org.springframework.web.bind.annotation.RestController;//表示对外提供可访问接口的类 @RestController public class WebController {}
<3>WebController类的hello()方法。(注解@GetMapping)
- @GetMapping("/hello")表明:WebController类的hello()方法会处理HTTP的GET请求,并且请求路径为"/hello"。
package com.hyl.controller;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;//表示对外提供可访问接口的类 @RestController public class WebController {@GetMapping("/hello")public String hello(){return "{'SpringBoot3':'Hello SpringBoot3!'}";}}
<4>重新启动工程并测试接口。
- 访问地址:localhost:9090/hello。
404访问资源未找到原因与解决方法。
在Spring Boot中,默认情况下带有@SpringBootApplication注解的主启动类会自动扫描其所在包及其子包下的组件。(如注解:@Controller、@Service、@Repository
@
等带有特定注解的类)
- 仔细观察本案例项目中,主启动类SpringBootApplication位于com.hyl.springboot包下,而WebController类位于com.hyl.controller包下,com.hyl.controlle并非com.hyl.springboot的子包。
- 所以可能会导致WebController类没有被正确扫描到并注册到Spring 容器中,进而使得访问相关路径时出现 404 错误。
移动SpringBootApplication启动类。
- 将SpringBootApplication启动类移动到com.hyl包下即可。
- 删去原来的springboot包。
重新启动工程再次访问localhost:9090/hello。
<5>接口中返回Integer类型的方法。
package com.hyl.controller;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;//表示对外提供可访问接口的类 @RestController public class WebController {@GetMapping("/hello")public String hello(){return "{'SpringBoot3':'Hello SpringBoot3!'}";}@GetMapping("/count")public Integer count(){return 666;}}
- 访问地址栏:localhost:9090/count。
<6>接口中返回Map<String,Object>类型的方法。
@GetMapping("/map")public Map<String,Object> map(){Map<String,Object> map = new HashMap();map.put("username","岁岁岁平安");map.put("age",18);return map;}
- 访问地址栏:localhost:9090/map。
(5)封装访问接口的结果类。(状态码code、请求信息msg、返回数据data)
<1>新建统一封装结果类。(Result类)
- 请求状态码:code。(200:请求成功。500:系统错误。400:业务错误)
- 请求是否成功信息。msg。(如果请求成功,返回正确信息。请求失败,返回错误信息!)
- 返回数据。data。(如果请求的方法有返回结果数据,就封装到data变量中并返回前端)
<2>提供对应的getter、setter方法。
//getter、setter方法public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}
<3>配置成功(success)静态方法。(成功、有无数据返回)
请求成功且无数据返回。
//请求成功且无数据返回public static Result success(){Result result = new Result();result.setCode("200");result.setMsg("请求成功");return result;}
请求成功且有数据返回。
//请求成功且有数据返回。public static Result success(Object data){//直接调用静态方法设置状态码、请求成功信息Result result = Result.success();result.setData(data);return result;}
修改接口中的方法。使用封装类Result封装结果集。
- 修改返回Integer类型的count()方法。
@GetMapping("/count")public Result count(){return Result.success(666);}
- 修改返回String类型的hello()方法。
@GetMapping("/hello")public Result hello(){return Result.success("hello");}
- 再次修改返回Map<String,Object>类型的map()方法。
@GetMapping("/map")public Result map(){Map<String,Object> map = new HashMap();map.put("username","岁岁岁平安");map.put("age",18);return Result.success(map);}
<4>配置失败(error)静态方法。(失败、异常信息返回)
- 如果请求的方法出现了异常(错误)。返回错误信息。
请求失败且无数据返回。
//请求失败且无数据返回public static Result error(){Result result = new Result();result.setCode("500");result.setMsg("系统出错了!");return result;}
全局异常处理器代码。(处理所有系统出现的异常!)
- 新建包:exception。新建类GlobalExceptionHandler。
package com.hyl.exception;import com.hyl.common.Result; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody;//标识全局异常处理器并设置捕获路径 //因为异常产生最终都会归宿到controller中,所有异常统一在controller包中接口中处理。 @ControllerAdvice("com.hyl.controller") public class GlobalExceptionHandler {@ExceptionHandler(Exception.class) //捕获所有系统异常@ResponseBody//返回json串public Result error(Exception e) {//更清晰看到后端控制台报的错误e.printStackTrace();//返回Result的error()方法设置的错误信息(code、msg)给前端return Result.error();} }
- 测试count()方法的算术异常。
@GetMapping("/count")public Result count(){int i = 1 / 0;return Result.success(666);}
- 访问localhost:9090/count。出现异常。并成功向前端返回对应的错误信息!
- 总分证明了全局异常处理器已经生效且成功捕获到系统产生的异常!
手动限制访问路径。(使用自定义异常)
- 修改count()方法。在方法中手动抛出Runtime Exception。此时就不是系统错误或者代码的逻辑错误。(不是程序的bug)。是后台自己定义的逻辑。当访问该路径,被禁止访问。
- 这时就需要手动的自定义异常。向用户、前端告知不是系统异常而是业务错误。
- 对应的接口文档设置状态码:系统错误:500。业务错误:400。
自定义异常类代码。
package com.hyl.exception;public class CustomerException extends Exception{private String code;private String msg;public CustomerException(String msg, String code) {this.msg = msg;this.code = code;}//getter|setter方法public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;} }
全局异常处理器类中——>配置自定义异常类。
package com.hyl.exception;import com.hyl.common.Result; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody;//标识全局异常处理器并设置捕获路径 //因为异常产生最终都会归宿到controller中,所有异常统一在controller包中接口中处理。 @ControllerAdvice("com.hyl.controller") public class GlobalExceptionHandler {@ExceptionHandler(Exception.class) //捕获所有系统异常@ResponseBody//返回json串public Result error(Exception e) {//更清晰看到后端控制台报的错误e.printStackTrace();//返回Result的error()方法设置的错误信息(code、msg)给前端return Result.error();}@ExceptionHandler(CustomerException.class) //捕获自定义异常@ResponseBody//返回json串public Result error(CustomerException e) { //自定义异常的参数code、msg//更清晰看到后端控制台报的错误e.printStackTrace();//Result的error(String code,String Msg)方法设置获取到的错误信息(code、msg)return Result.error(e.getCode(),e.getMsg());}}
请求失败且无参数返回。(有参数)
//请求失败且无数据返回。适配自定义异常(传递code、msg)public static Result error(String code,String msg){Result result = new Result();result.setCode(code);result.setMsg(msg);return result;}
- 再次重新测试count()方法。count()抛出自定义异常。
@GetMapping("/count")public Result count() throws CustomerException {throw new CustomerException("400","错误!禁止访问该路径");}
- 全局异常处理器中成功捕获自定义异常。并向前端返回400状态码、错误信息。
<5>最终的自定义封装结果类(Result)。
package com.hyl.common;/*** 统一后端返回的数据结果类型*/ public class Result {//请求状态码private String code;//成功或错误信息private String msg;//返回数据结果private Object data;//常用静态方法//请求成功且无数据返回public static Result success(){Result result = new Result();result.setCode("200");result.setMsg("请求成功");return result;}//请求成功且有数据返回。public static Result success(Object data) {//直接调用静态方法设置状态码、请求成功信息Result result = Result.success();result.setData(data);return result;}//请求失败且无数据返回public static Result error(){Result result = new Result();result.setCode("500");result.setMsg("系统出错了!");return result;}//请求失败且无数据返回。适配自定义异常(传递code、msg)public static Result error(String code,String msg){Result result = new Result();result.setCode(code);result.setMsg(msg);return result;}//getter、setter方法public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public Object getData() {return data;}public void setData(Object data) {this.data = data;} }