1.添加pom
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
com.github.ulisesbocchio
jasypt-spring-boot-starter
2.1.0
org.projectlombok
lombok
true
2.设置统一返回类型工具类
package com.example.util;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
@Data
public class ReturnResult {
private Boolean success;
private Integer code;
private String message;
private Map data = new HashMap<>();
private ReturnResult(){}
public static ReturnResult ok(){
ReturnResult returnResult = new ReturnResult();
returnResult.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
returnResult.setCode(ResultCodeEnum.SUCCESS.getCode());
returnResult.setMessage(ResultCodeEnum.SUCCESS.getMessage());
return returnResult;
}
public static ReturnResult error(){
ReturnResult returnResult = new ReturnResult();
returnResult.setSuccess(ResultCodeEnum.ERROR.getSuccess());
returnResult.setCode(ResultCodeEnum.ERROR.getCode());
returnResult.setMessage(ResultCodeEnum.ERROR.getMessage());
return returnResult;
}
public static ReturnResult setResult(ResultCodeEnum resultCodeEnum){
ReturnResult returnResult = new ReturnResult();
returnResult.setSuccess(resultCodeEnum.getSuccess());
returnResult.setCode(resultCodeEnum.getCode());
returnResult.setMessage(resultCodeEnum.getMessage());
return returnResult;
}
public ReturnResult success(Boolean success){
this.setSuccess(success);
return this;
}
public ReturnResult message(String message){
this.setMessage(message);
return this;
}
public ReturnResult code(Integer code){
this.setCode(code);
return this;
}
public ReturnResult data(String key, Object value){
this.data.put(key, value);
return this;
}
public ReturnResult data(Map map){
this.setData(map);
return this;
}
}
3.设置code工具类
package com.example.util;
import lombok.Getter;
@Getter
public enum ResultCodeEnum {
SUCCESS(true, 200,"成功"),
ERROR(false,500,"服务器发现异常"),
UNKNOWN_REASON(false, 505, "未知错误"),
BAD_SQL_GRAMMAR(false, 303, "sql语法错误"),
JSON_PARSE_ERROR(false, 221, "json解析异常"),
PARAM_ERROR(false, 210, "参数不正确"),
FILE_UPLOAD_ERROR(false, 233, "文件上传错误,选择正确的格式"),
EXCEL_DATA_IMPORT_ERROR(false, 215, "Excel数据导入错误"),
ARITHMETIC_ERROR(false,211,"算数异常!不能除0"),
TEACHER_NULL_ERROR(false, 500, "没有该讲师,请重新输入!"),
FILE_NULL_ERROR(false,500,"文件内容不正确"),
FETCH_PLAYAUTH_ERROR(false,404,"播放错误"),
NULL_ERROR(false,500,"空指针异常")
;
private Boolean success;
private Integer code;
private String message;
private ResultCodeEnum(Boolean success, Integer code, String message) {
this.success = success;
this.code = code;
this.message = message;
}
}
4.设置捕捉全局异常类
@ControllerAdvice
@Slf4j
@ResponseBody
public class ExceptionHandlers {
@ExceptionHandler(Exception.class)
public ReturnResult error(Exception e){
String failMsg=null;
e.printStackTrace();
if (e instanceof BindException){
failMsg = ((BindException) e).getBindingResult().getFieldError().getDefaultMessage();
return ReturnResult.error().data("服务器出现异常:",failMsg);
}
return ReturnResult.error().data("发现异常信息为:",e.getMessage());
}
}
5.设置自定义异常类
@Slf4j
@Data
public class LinliException extends RuntimeException {
private Integer code;
public LinliException(Integer code, String message) {
super(message);
this.code=code;
}
public LinliException(ResultCodeEnum resultCodeEnum){
super(resultCodeEnum.getMessage());
log.error(resultCodeEnum.getMessage());
this.code=resultCodeEnum.getCode();
}
@Override
public String toString() {
return "GuliException{" +
"message=" + this.getMessage() +
", code=" + code +
'}';
}
}
6.编写controller测试
@RestController
public class ExceptionController {
@RequestMapping("/excption")
public ReturnResult Excption(){
int a=12/0;
return ReturnResult.ok();
}
@GetMapping("/excption2/{name}")
public ReturnResult returnResult(@PathVariable("name") String name){
if (!"zhangsan".equals(name)){
GuliException exception = new GuliException(ResultCodeEnum.TEACHER_NULL_ERROR);
return ReturnResult.error().message(exception.getMessage());
// new GuliException(500,"没有该名字!");
}
return ReturnResult.ok();
}
}
7.访问地址测试