一、创建项目
生成
二、精化
1.删除
2.还有src里的test目录;
三、配置maven
(一)、maven可以搜索官网下载自己需要的版本:maven官网
1.删除resources里面的static,templates
2.删除pom.xml里面的无用依赖
(二)、代码结构分析
(1) .idea:idea软件的配置文件(不用管);
(2) src:源码的目录
(3)SpringbootApplication:工程的启动类
(4)application.yml:是Springboot的配置文件
(5)target:源码编译后的文件(可以忽略)
(6)pom.xml:定义了Springboot工程的所有依赖项,Springboot加载时会扫描这个文件里面所有的依赖项,然后下载
Springboot里面内置了Tomcat
application.properties————>application.yml
到这步的时候你的精简版springboot3+maven已经搭建好了
四、常见点
统一返回包装类 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.setData("请求成功");
return result;
}
public static Result success(Object data) {
Result result = new Result();
result.setCode("200");
result.setData(data);
result.setData("请求成功");
return result;
}
public static Result error(String msg) {
Result result = new Result();
result.setCode("500");
result.setMsg(msg);
return result;
}
public static Result success(String code, String msg) {
Result result = new Result();
result.setCode(code);
result.setData(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;
}
}
500错误 常见的系统错误
全局异常处理、
新建excption包再建GlobalExceptionHandler
package com.example.excption;
import com.example.common.Result;
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 {
@ExceptionHandler(Exception.class)
@ResponseBody // 将result对象转换成 json的格式
public Result error(Exception e) {
return Result.error("系统异常");
}
}
自定义异常
CustomerException
package com.example.excption;
/**
* 自定义异常
* 运行时异常
*/
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 getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
全局异常最终版
package com.example.excption;
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());
}
}
五、若是想结合vue3工程
1.重新配置依赖
重新打开到code工程(也就是自己的vue3项目),同时加载springboot
2.设置编码
到这步的时候你的vue3项目就嵌合springboot3了