一、前情提要
1.自启动 自动跳入首页:
{path: '/', redirect: '/manager/home'},/*直接到达后台主页*/
验证:
2.编程软件 :
IDEA2023 以上的版本、Maven3.8、JDK21(大于17版本即可)
学习SpringBoot3 的基础必备知识:JavaSE、JavaWeb
二、创建Spring Boot工程
新建pringBoot3工程
删除无用文件
源码在Src和pom.xml文件内
三、代码结构分析
文件细致讲解:
idea:idea软件的配置文件(不用管它)
src:源码的目录
gbootApplication:工程的启动类
application.yml: Springboot 的配置文件springbootApplication启动类:启动springboot工程点击run就可以启动springbot默认段某8080
application.yml:Springboot 的配置文件
target:源码编译后的文件目录(可以忽略)
pom.xml:定义Springboot工程的所有依赖项,springboot加载的时候会扫描这个文件里面所有的依赖项,然后下载所需的依赖项
干净的代码结构:
配置端口号:
格式化port两个空格
重新打开code2025目录,同时加载springboot和vue
然后关闭项目工程,打开code2025:
相当于可以在一个项目内修改vue和springboot,得到:
删除.idea文件:
选择pom.xml ,右键点击加载依赖:
验证:
设置编码:
启动springboot项目:
新建测试接口
在com.example下右键新建包controller
通常来说会建立三个模块,controller(测试接口)、service(服务端口)、mapper(对接数据库)
建WebController.java:
usera所有用的文件经webCongoellrestCOpntyrollerdignyicontreoller
controller不能对外提供数据
写一个"say hello"接口
“/”代表路由
接口的一般结构:
接口路径(斜杠后边写英文,且不同路由路径不能重复)
写完一个接口之后必须重启:
package com.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author mieMie
* @Version 1.0
*/
@RestController
public class WebController {
//表示这是一个get请求接口
@GetMapping("/")//接口的路径,全局唯一
public String hello(){
return "hello";//接口返回值
}
}
重启输入localhost:9999(你设置的端口号)得到:
如果出现了404,说明根据路径没请求到接口数据,检查路由路径、端口号,修改后重启
查看测试页面网络请求:
得到:
统一返回包装类Result
包装类:作用是统一后端返回的数据类型,code是作为前端判断请求成功的依据,msg是错误的信息,data是返回给前端的数据
WebController.java内容:
package com.example.controller;
import com.example.common.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author mieMie
* @Version 1.0
*/
@RestController
public class WebController {
//表示这是一个get请求接口
@GetMapping("/hello")//接口的路径,全局唯一
public Result hello(){
Result result = new Result();
result.setCode("200");
result.setData("hello");
return result;//接口返回值
}
}
Result.java内容:
package com.example.common;
/**
* @Author mieMie
* @Version 1.0
*/
public class Result {
private String code;
private String data; // 将 date 改为 data,并设置为 String 类型
private String msg;
// Getter and Setter for code
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
// Getter and Setter for data (原 date)
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
// Getter and Setter for msg
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
得到:
分模块进行修改
Result.java内容:
package com.example.common;
/**
* @Author mieMie
* @Version 1.0
*/
public class Result {
private String code;
private String data; // 将 date 改为 data,并设置为 String 类型
private String msg;
// Getter and Setter for code
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
// Getter and Setter for data (原 date)
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
// Getter and Setter for msg
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
新建ResultUtil.java内容,将统一返回分块进行,此内容为静态模块,所以需要新建一个类型
package com.example.common;
public class ResultUtil {
public static Result success() {
Result result = new Result();
result.setCode("200");
result.setMsg("请求成功");
return result;
}
public static Result success(Object data) {
Result result = new Result();
result.setCode("200");
result.setMsg("请求成功");
result.setData(data.toString()); // 将 Object 转换为 String
return result;
}
public static Result error(String msg) {
Result result = new Result();
result.setCode("500");
result.setMsg(msg);
return result;
}
}
适当修改WebController.java内容:
package com.example.controller;
import com.example.common.Result;
import com.example.common.ResultUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WebController {
@GetMapping("/hello")
public Result hello() {
return ResultUtil.success();
}
@GetMapping("/hello/data")
public Result helloWithData() {
return ResultUtil.success(new Object());
}
@GetMapping("/error")
public Result error() {
return ResultUtil.error("发生错误");
}
}
得到:
统一返回数据:
系统异常处理:
看你的控制台,然后找到报错的代码位置,具体地去修正代码
1.AdminService模块:
package com.example.service;
import org.springframework.stereotype.Service;
/**
* @Author mieMie
* @Version 1.0
*/
public class AdminService {
public String admin(String name){
if("admin".equals(name)){
return "admin";
}else{
return "error";
}
}
}
2.WebController模块:
package com.example.controller;
import com.example.common.Result;
import com.example.common.ResultUtil;
import com.example.service.AdminService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
@RestController
public class WebController {
@Resource
AdminService adminService;
//
@GetMapping("/admin")
public Result admin(String name) {
String admin = adminService.admin(name);
return ResultUtil.success(admin);
}
@GetMapping("/hello")
public Result hello() {
return ResultUtil.success();
}
@GetMapping("/hello/data")
public Result helloWithData() {
return ResultUtil.success(new Object());
}
@GetMapping("/error")
public Result error() {
return ResultUtil.error("发生错误");
}
}
全局异常最终版本:
1.GlobalExceptionHandler模块内容:
package com.example.exception;
import com.example.common.ResultUtil;
import com.example.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice("com.example.controller")
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(Exception.class)
@ResponseBody
public Result error(Exception e) {
log.error("服务器发生异常: {}", e.getMessage(), e);
return ResultUtil.error(500, "服务器发生异常"); // 使用新增的方法
}
@ExceptionHandler(CustomerException.class)
@ResponseBody
public Result customerError(CustomerException e) {
log.error("自定义异常: {}", e.getMessage(), e);
return ResultUtil.error(e.getCode(), e.getMsg()); // 使用新增的方法
}
}
2.CustomerException 模块内容:
package com.example.exception;
/**
* @Author mieMie
* @Version 1.0
* 自定义异常
* 运行时异常
*/
public class CustomerException extends RuntimeException {
private int code;
private String msg;
public CustomerException(String code, String msg) {
this.code = Integer.parseInt(code); // 将字符串转换为整数
this.msg = msg;
}
public CustomerException(String msg) {
this.code = 500; // 直接赋值为整数
this.msg = msg;
}
public CustomerException() {}
public int getCode() {
return code;
}
public void setCode(int code) { // 修改为 int 类型
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
3.ResultUtil内容:
package com.example.common;
public class ResultUtil {
public static Result success() {
Result result = new Result();
result.setCode("200");
result.setMsg("请求成功");
return result;
}
public static Result success(Object data) {
Result result = new Result();
result.setCode("200");
result.setMsg("请求成功");
result.setData(data.toString()); // 将 Object 转换为 String
return result;
}
public static Result error(String msg) {
Result result = new Result();
result.setCode("500");
result.setMsg(msg);
return result;
}
// 新增的方法,支持两个参数
public static Result error(int code, String msg) {
Result result = new Result();
result.setCode(String.valueOf(code)); // 将 int 转换为 String
result.setMsg(msg);
return result;
}
}
测试得到:
大功告成!!!