Springboot3框架的快速搭建
编程软件
IDEA2023 以上的版本、Maven3.8、JDK21
创建 SpringBoot
新建 SpringBoot3 工程

删除无用文件

删除test目录
删除 resources 里面的 static 和 templates 文件
删除 pom.xml 里面的无用代码

配置maven

代码结构分析

.idea:idea软件的配置文件
src:源码目录
SpringbootApplication:工程的启动类
application.yml:Springboot 的配置文件
target:编译后的文件目录(可以忽略)
pom.xml:定义 springboot 工程的所有依赖项,springboot 加载的时候会扫描这个文件里面所有的依赖项,然后下载它所需要的依赖项
application.properties -> application.yml


SpringBoot 里面内置了 Tomact,不需要再单独下载
重新打开code目录,同时加载 springboot 和 vue

加载 maven 依赖
删除springboot下的.idea文件。

选择 pom.xml 然后右键点击 Add as Maven Project 加载依赖。
设置编码

启动SpringBoot工程

写一个测试接口 say:hello
package com.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WebController {
//表示这是一个get请求的接口
@GetMapping("/")//接口的路径,全局唯一的
public String hello() {
return "Hello";//接口的返回值
}
}
修改完代码后记得重启后端
统一返回包装类 Result
包装类:作用是统一后端返回的数据类型,code是作为前端判断请求成功的依据,msg是错误的信息,data是返回给前端的数据
package com.example.common;
/**
* 统一返回的包装类
*/
public class Result {
private String code;//告诉前端请求成功还是失败
private Object data;//后端返回给前端的数据
private String msg;//将错误信息提示给用户
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.setData(data);
result.setMsg("请求成功");
return result;
}
public static Result error(String msg) {
Result result = new Result();
result.setCode("500");
result.setMsg(msg);
return result;
}
public static Result error(String code,String msg) {
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
return result;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
全局异常处理
GlobalExceptionHandler
package com.example.exception;
import com.example.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 全局异常捕获器
*/
@ControllerAdvice("com.example.controller")
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(Exception.class)
@ResponseBody//将result对象转换成 json 格式
public Result error(Exception e) {
log.error("系统异常",e);
return Result.error("系统异常");
}
@ExceptionHandler(CustomerException.class)
@ResponseBody//将result对象转换成 json 格式
public Result customerError(CustomerException e) {
log.error("自定义错误",e);
return Result.error(e.getCode(),e.getMsg());
}
}
自定义异常
CustomerException
package com.example.exception;
/**
* 自定义异常
* 运行时异常
*/
public class CustomerException extends RuntimeException{
private String code;
private String msg;
public CustomerException(String code, String msg) {
this.code = code;
this.msg = msg;
}
public CustomerException(String msg) {
this.code = "500";
this.msg = msg;
}
public CustomerException() {}
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;
}
}

浙公网安备 33010602011771号