处理异常的更好方法是spring引导
我有大约十个API,它与redis对话以存储数据 目前它正在抛出嵌套异常,所以我已经按照下面的方法处理了嵌套异常处理异常的更好方法是spring引导,spring,spring-boot,exception,Spring,Spring Boot,Exception,我有大约十个API,它与redis对话以存储数据 目前它正在抛出嵌套异常,所以我已经按照下面的方法处理了嵌套异常 @Override public boolean delMyStatus(String key) { try{ return redisTemplate.delete(key); } catch (Exception e){ if(e.getCause() != nul
@Override
public boolean delMyStatus(String key) {
try{
return redisTemplate.delete(key);
}
catch (Exception e){
if(e.getCause() != null && e.getCause().getCause() instanceof RedisException) {
RedisException ex = (RedisException)e.getCause().getCause();
log.error("RedisException " + ex.getMessage());
/* Do some action*/
} else {
throw new IllegalStateException("...");
}
}
return false;
}
但我不想对redis dao的所有API都这样做,有没有更好的方法来处理异常 您可以使用
@RestControllerAdvice
。从每个控制器创建一个自定义异常类CustomRedisException
抛出CustomRedisException
异常,并在单独的类中处理该异常,并用@RestControllerAdvice
注释
@Override
public boolean delMyStatus(String key) {
try{
return redisTemplate.delete(key);
}
catch (Exception e){
if(e.getCause() != null && e.getCause().getCause() instanceof RedisException) { RedisException ex = (RedisException)e.getCause().getCause();
throw new CustomRedisException(ex);
} else {
throw new IllegalStateException("...");
}
}
return false;
}
使GlobalExceptionHandler如下所示
@RestControllerAdvice(basePackages = "your base package here", basePackageClasses = RepositoryRestExceptionHandler.class)
public class GlobalRestExceptionHandler {
@ExceptionHandler
public ResponseEntity<ErrorResponse> handleCustomException(final CustomRedisExceptionex) {
// code for exception handling here.
return new ResponseEntity<>(
new ErrorResponse(HttpStatus.PRECONDITION_FAILED.value(), ex.getMessage()),
HttpStatus.PRECONDITION_FAILED);
}
}
@RestControllerAdvice(basePackages=“您的基本包在这里”,basePackageClasses=repositoryresteptionhandler.class)
公共类GlobalRestExceptionHandler{
@例外处理程序
公共响应handleCustomException(最终CustomRedisExceptionex){
//这里是异常处理的代码。
返回新响应(
新的错误响应(HttpStatus.Premission_FAILED.value(),例如getMessage()),
HttpStatus.Premission_失败);
}
}
您可以使用@RestControllerAdvice
。从每个控制器创建一个自定义异常类CustomRedisException
抛出CustomRedisException
异常,并在单独的类中处理该异常,并用@RestControllerAdvice
注释
@Override
public boolean delMyStatus(String key) {
try{
return redisTemplate.delete(key);
}
catch (Exception e){
if(e.getCause() != null && e.getCause().getCause() instanceof RedisException) { RedisException ex = (RedisException)e.getCause().getCause();
throw new CustomRedisException(ex);
} else {
throw new IllegalStateException("...");
}
}
return false;
}
使GlobalExceptionHandler如下所示
@RestControllerAdvice(basePackages = "your base package here", basePackageClasses = RepositoryRestExceptionHandler.class)
public class GlobalRestExceptionHandler {
@ExceptionHandler
public ResponseEntity<ErrorResponse> handleCustomException(final CustomRedisExceptionex) {
// code for exception handling here.
return new ResponseEntity<>(
new ErrorResponse(HttpStatus.PRECONDITION_FAILED.value(), ex.getMessage()),
HttpStatus.PRECONDITION_FAILED);
}
}
@RestControllerAdvice(basePackages=“您的基本包在这里”,basePackageClasses=repositoryresteptionhandler.class)
公共类GlobalRestExceptionHandler{
@例外处理程序
公共响应handleCustomException(最终CustomRedisExceptionex){
//这里是异常处理的代码。
返回新响应(
新的错误响应(HttpStatus.Premission_FAILED.value(),例如getMessage()),
HttpStatus.Premission_失败);
}
}
您可以通过方面和@后面的注释来实现它
首先确保允许Spring在任何配置类上使用@enableaspectProxy
注释来使用方面
然后定义一个@Aspect
类,方法用@posterhrowing
注释如下:
@Aspect
public class GenericExceptionHandler {
// you can use more specific path here
@AfterThrowing ("execution(* *.*(..))", throwing = "ex")
public void handleException(Exception ex) throws Exception {
// handle the exception here
}
}
您可以通过aspects和@后序
注释来实现它
首先确保允许Spring在任何配置类上使用@enableaspectProxy
注释来使用方面
然后定义一个@Aspect
类,方法用@posterhrowing
注释如下:
@Aspect
public class GenericExceptionHandler {
// you can use more specific path here
@AfterThrowing ("execution(* *.*(..))", throwing = "ex")
public void handleException(Exception ex) throws Exception {
// handle the exception here
}
}
catch(RedisException e){}
?catch(RedisException e){log.error(“RedisException”+ex.getMessage());}catch(Exception e){log.error(“Exception”+ex.getMessage());}这总是会导致异常块catch(RedisException e){
?catch(RedisException e){log.error(“RedisException”+ex.getMessage();}catch(异常e){log.error(“异常”+ex.getMessage());}这总是会导致异常阻塞实际上我不想将此错误返回给rest控制器,这是为了一些内部操作,所以我想在内部处理此错误并在此基础上执行一些操作在这种情况下,您可以创建一个通用方法来处理异常,并从每个catch块调用它。实际上,我不想将此错误返回给rest控制器,它是用于某些内部操作,因此我想在内部处理此错误,并在此基础上执行一些操作在这种情况下,您可以创建一个公共方法来处理异常,并从每个catch块调用它。那么,如何将用户参数连同异常一起发送到@Afterhrowing函数呢?在这里,我想记录调用方发送的一些参数。那么,如何将用户参数和异常一起发送给@Afterhrowing函数呢