Springbootグローバル例外処理クラスの定義GlobalExceptionHandler
グローバル例外処理:GlobalExceptionHandlerグローバル例外処理クラス を定義する.以下は拡張部 である. は、パラメータパッケージ を返す.参照例
グローバル例外処理クラスの定義
注意:ページに戻る必要がある場合は、@ResponseBody注記を削除します.
以下は拡張部分
ログ印刷@Slf 4 jと@Dataはlombokを使用しています
戻りパラメータカプセル
注意:カスタマイズ可能
参照例
グローバル例外処理クラスの定義
注意:ページに戻る必要がある場合は、@ResponseBody注記を削除します.
package com.xiyan.mydemo.common.config;
import com.xiyan.mydemo.common.utils.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @Author xiyan
* @Date 2020/6/11
* @Version 1.0
*/
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
/**
*
* @param e
* @return
*/
@ResponseBody
@ExceptionHandler
public ApiResult unknowException(Exception e){
log.info(" :"+e.getMessage());
e.printStackTrace();//
return ApiResult.unknowError();
}
/**
*
* @param e
* @return
*/
@ResponseBody
@ExceptionHandler(RuntimeException.class)
public ApiResult runtimeExceptionHandler(final RuntimeException e) {
log.info(" :"+e.getMessage());
e.printStackTrace();//
return ApiResult.runtimeError(e.getMessage());
}
/**
*
* @param e
* @return
*/
@ResponseBody
@ExceptionHandler(CustomException.class)
public ApiResult customExceptionHandler(final CustomException e) {
log.info(" :"+e.getMessage());
e.printStackTrace();//
return ApiResult.error(e.getMessage());
}
}
以下は拡張部分
ログ印刷@Slf 4 jと@Dataはlombokを使用しています
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
戻りパラメータカプセル
注意:カスタマイズ可能
package com.xiyan.mydemo.common.utils;
import lombok.Data;
/**
*
* @Author xiyan
* @Date 2020/6/11
* @Version 1.0
*/
@Data
public class ApiResult {
private Boolean flag;
private Integer code;
private String msg;
private Object data;
public ApiResult(Boolean flag, Integer code, String msg, Object data) {
super();
this.flag = flag;
this.code = code;
this.msg = msg;
this.data = data;
}
public ApiResult(Boolean flag, Integer code, String msg) {
super();
this.flag = flag;
this.code = code;
this.msg = msg;
}
public static ApiResult success() {
return new ApiResult(Boolean.TRUE,200," ");
}
public static ApiResult success(Object data) {
return new ApiResult(Boolean.TRUE,200," ",data);
}
/**
*
* @param msg
* @return
*/
public static ApiResult error(String msg) {
return new ApiResult(Boolean.FALSE,201,msg);
}
public static ApiResult unknowError() {
return new ApiResult(Boolean.FALSE,500," , ");
}
public static ApiResult runtimeError(String msg) {
return new ApiResult(Boolean.FALSE,400,msg);
}
}
参照例
@Override
public int insertEntity(UserInfoVO vo) {
if (StringUtils.isBlank(vo.getUsername()) || StringUtils.isBlank(vo.getPassword())) {
throw new RuntimeException(" ");
}
if(!RegexUtils.checkUsername(vo.getUsername())){
throw new CustomException(RegexUtils.REGEX_USERNAME_ERROR);
}
if(!RegexUtils.checkPasswprd(vo.getPassword())){
throw new RuntimeException(RegexUtils.REGEX_PASSWORD_ERROR);
}
if(userInfoMapper.selectOneByUserName(vo.getUsername())){
throw new RuntimeException(" ("+vo.getUsername()+") ");
}
String salt = randomSalt();
UserInfo entity = UserInfo.builder().userId(GenarateIdUtils.getUserId()).username(vo.getUsername())
.salt(salt).password(encryptPassword(vo.getUsername(), vo.getPassword(), salt)).build();
return userInfoMapper.insertEntity(entity);
}