修复系统异常时返回的响应是404的问题
This commit is contained in:
parent
fa7435e95b
commit
f92ac3be80
|
|
@ -1,17 +1,18 @@
|
||||||
package com.nflg.product.base.core.exception;
|
package com.nflg.product.base.core.exception;
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import nflg.product.common.constant.STATE;
|
import nflg.product.common.constant.STATE;
|
||||||
import nflg.product.common.vo.ResultVO;
|
import nflg.product.common.vo.ResultVO;
|
||||||
import org.springframework.dao.DataIntegrityViolationException;
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
import org.springframework.dao.DuplicateKeyException;
|
import org.springframework.dao.DuplicateKeyException;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.jdbc.BadSqlGrammarException;
|
import org.springframework.jdbc.BadSqlGrammarException;
|
||||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
|
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||||
|
|
@ -33,31 +34,35 @@ public class BaseGlobalExceptionHandle {
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(value = NflgBusinessException.class)
|
@ExceptionHandler(value = NflgBusinessException.class)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResultVO<String> handleJeecgBootException(NflgBusinessException e) {
|
public ResponseEntity<ResultVO<String>> handleJeecgBootException(NflgBusinessException e) {
|
||||||
return ResultVO.error(e.getState(), e.getMessage());
|
log.error(e.getMessage(), e);
|
||||||
|
//return ResultVO.error(e.getState(), e.getMessage());
|
||||||
|
return errorResult(e.getState(),e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(value = NoHandlerFoundException.class)
|
@ExceptionHandler(value = NoHandlerFoundException.class)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResultVO<String> handlerNoFoundException(NoHandlerFoundException e) {
|
public ResponseEntity<ResultVO<String>> handlerNoFoundException(NoHandlerFoundException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
return ResultVO.error(404, "路径不存在,请检查路径是否正确");
|
//return ResultVO.error(404, "路径不存在,请检查路径是否正确");
|
||||||
|
return errorResult(STATE.AddressNotFound,"路径不存在,请检查路径是否正确");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(value = DuplicateKeyException.class)
|
@ExceptionHandler(value = DuplicateKeyException.class)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResultVO<String> handleDuplicateKeyException(DuplicateKeyException e) {
|
public ResponseEntity<ResultVO<String>> handleDuplicateKeyException(DuplicateKeyException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
return ResultVO.error("已存在该记录,请勿重复操作");
|
//return ResultVO.error("已存在该记录,请勿重复操作");
|
||||||
|
return errorResult("已存在该记录,请勿重复操作");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ExceptionHandler(value = Exception.class)
|
@ExceptionHandler(value = Exception.class)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResultVO<String> handleException(Exception e) {
|
public ResponseEntity<ResultVO<String>> handleException(Exception e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
return ResultVO.error("操作失败,请联系系统管理员");
|
//return ResultVO.error("操作失败,请联系系统管理员");
|
||||||
|
return errorResult("操作失败,请联系系统管理员");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -67,8 +72,9 @@ public class BaseGlobalExceptionHandle {
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
|
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResultVO<String> HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
|
public ResponseEntity<ResultVO<String>> HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
|
||||||
StringBuffer sb = new StringBuffer();
|
log.error(e.getMessage(), e);
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("不支持");
|
sb.append("不支持");
|
||||||
sb.append(e.getMethod());
|
sb.append(e.getMethod());
|
||||||
sb.append("请求方法,");
|
sb.append("请求方法,");
|
||||||
|
|
@ -81,8 +87,8 @@ public class BaseGlobalExceptionHandle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.error(sb.toString(), e);
|
log.error(sb.toString(), e);
|
||||||
return ResultVO.error(STATE.PermissionDenied, "登录已过期,请重新登录");
|
//return ResultVO.error(STATE.PermissionDenied, "登录已过期,请重新登录");
|
||||||
// return ResultVO.error(STATE.PermissionDenied, sb.toString());
|
return errorResult(STATE.PermissionDenied,"不支持的请求方法:"+e.getMethod());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -90,53 +96,69 @@ public class BaseGlobalExceptionHandle {
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(value = MaxUploadSizeExceededException.class)
|
@ExceptionHandler(value = MaxUploadSizeExceededException.class)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResultVO<String> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
|
public ResponseEntity<ResultVO<String>> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
return ResultVO.error("文件大小超出10MB限制, 请压缩或降低文件质量! ");
|
//return ResultVO.error("文件大小超出10MB限制, 请压缩或降低文件质量! ");
|
||||||
|
return errorResult("文件大小超出10MB限制, 请压缩或降低文件质量! ");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(value = DataIntegrityViolationException.class)
|
@ExceptionHandler(value = DataIntegrityViolationException.class)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResultVO<String> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
|
public ResponseEntity<ResultVO<String>> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
return ResultVO.error("字段太长,超出数据库字段的长度");
|
// return ResultVO.error("字段太长,超出数据库字段的长度");
|
||||||
|
return errorResult("字段太长,超出数据库字段的长度");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(value = SQLException.class)
|
@ExceptionHandler(value = SQLException.class)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResultVO<String> handleSQLException(SQLException e) {
|
public ResponseEntity<ResultVO<String>> handleSQLException(SQLException e) {
|
||||||
log.error(e.getMessage(),e);
|
log.error(e.getMessage(),e);
|
||||||
return ResultVO.error("sql执行过程出现错误");
|
//return ResultVO.error("sql执行过程出现错误");
|
||||||
|
return errorResult("sql执行过程出现错误");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(value = RuntimeException.class)
|
@ExceptionHandler(value = RuntimeException.class)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResultVO<String> handleRuntimeExceptionException(RuntimeException e) {
|
public ResponseEntity<ResultVO<String>> handleRuntimeExceptionException(RuntimeException e) {
|
||||||
log.error(e.getMessage(),e);
|
log.error(e.getMessage(),e);
|
||||||
throw new NflgBusinessException(STATE.Error, "系统错误,请联系管理员");
|
//throw new NflgBusinessException(STATE.Error, "系统错误,请联系管理员");
|
||||||
|
return errorResult("系统错误,请联系管理员");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ExceptionHandler(value = BadSqlGrammarException.class)
|
@ExceptionHandler(value = BadSqlGrammarException.class)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResultVO<String> handleDataAccessException(BadSqlGrammarException e) {
|
public ResponseEntity<ResultVO<String>> handleDataAccessException(BadSqlGrammarException e) {
|
||||||
log.error(e.getMessage(),e);
|
log.error(e.getMessage(),e);
|
||||||
throw new NflgBusinessException(STATE.Error, "sql 语法错误,系统错误,请联系管理员");
|
//throw new NflgBusinessException(STATE.Error, "sql 语法错误,系统错误,请联系管理员");
|
||||||
|
return errorResult("sql 语法错误,系统错误,请联系管理员");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResultVO<String> handleMethodArgumentException(MethodArgumentNotValidException e){
|
public ResponseEntity<ResultVO<String>> handleMethodArgumentException(MethodArgumentNotValidException e){
|
||||||
log.error(e.getMessage(),e);
|
log.error(e.getMessage(),e);
|
||||||
return ResultVO.error(e.getBindingResult().getFieldError().getDefaultMessage()) ;
|
return errorResult(e.getBindingResult().getFieldError().getDefaultMessage()) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(NullPointerException.class)
|
@ExceptionHandler(NullPointerException.class)
|
||||||
public ResultVO<String> handleNullPointerException(NullPointerException ex) {
|
public ResponseEntity<ResultVO<String>> handleNullPointerException(NullPointerException ex) {
|
||||||
|
log.error(ex.getMessage(),ex);
|
||||||
return ResultVO.error(STATE.Error, "系统错误,请联系管理员:"+ex.getMessage()) ;
|
return errorResult(STATE.Error, "系统错误,请联系管理员:"+ex.getMessage()) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(MissingServletRequestParameterException.class)
|
||||||
|
public ResponseEntity<ResultVO<String>> handleMissingServletRequestParameterException(MissingServletRequestParameterException ex) {
|
||||||
|
log.error(ex.getMessage(),ex);
|
||||||
|
return errorResult(STATE.ParamErr, ex.getParameterName()+"参数不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ResponseEntity<ResultVO<String>> errorResult(String msg) {
|
||||||
|
return ResponseEntity.status(HttpStatus.OK).body(ResultVO.error(STATE.Error,msg));
|
||||||
|
}
|
||||||
|
private static ResponseEntity<ResultVO<String>> errorResult(STATE code,String msg) {
|
||||||
|
return ResponseEntity.status(HttpStatus.OK).body(ResultVO.error(code,msg));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue