优化404拦截写法

This commit is contained in:
TinyAnts
2022-11-16 23:21:59 +08:00
parent dafaf09011
commit e211426c1a
4 changed files with 29 additions and 13 deletions

View File

@@ -13,6 +13,7 @@ import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.NoHandlerFoundException;
import java.util.Objects;
@@ -38,6 +39,16 @@ public class GlobalException {
return AjaxResult.failed(HttpEnum.SYSTEM_ERROR.getCode(), e.getMessage());
}
/**
* 拦截404异常
*/
@ResponseStatus(HttpStatus.OK)
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseBody
public AjaxResult<Object> handleNoHandlerFoundException(NoHandlerFoundException e){
return AjaxResult.failed(HttpEnum.REQUEST_404_ERROR.getCode(), e.getMessage());
}
/**
* 拦截自定义抛出异常
*/