完善框架结构,解决了validation和saToken的问题

This commit is contained in:
guo
2024-07-14 23:00:59 +08:00
parent b1358eaca0
commit e759ab2a75
14 changed files with 315 additions and 64 deletions
@@ -0,0 +1,51 @@
package com.guo.learningprogresstracker.common;
import com.guo.learningprogresstracker.entity.CommonResult;
import com.guo.learningprogresstracker.exception.AppException;
import com.guo.learningprogresstracker.exception.ErrorParameterException;
import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
/**
* @author guo
*/
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler({ErrorParameterException.class})
@ResponseBody
public CommonResult errorParameterException(ErrorParameterException ex){
//code:400
return CommonResult.error(ex.getMessage());
}
@ExceptionHandler({NotFindEntitiesException.class})
@ResponseBody
public CommonResult notFindEntitiesException(NotFindEntitiesException ex){
//code:400
return CommonResult.error(ex.getMessage());
}
@ExceptionHandler({AppException.class})
@ResponseBody
public CommonResult AppException(AppException ex){
//code:500
return CommonResult.serverError(ex.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public CommonResult MyMethodArgumentNotValidException(MethodArgumentNotValidException ex){
BindingResult bindingResult = ex.getBindingResult();
//code:400
return CommonResult.error(bindingResult.getFieldError().getDefaultMessage());
}
}