Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧靴@ControllerAdvice+@事务处理未按预期工作_Java_Spring Boot_Transactional - Fatal编程技术网

Java 弹簧靴@ControllerAdvice+@事务处理未按预期工作

Java 弹簧靴@ControllerAdvice+@事务处理未按预期工作,java,spring-boot,transactional,Java,Spring Boot,Transactional,我在一个投注网站上工作,所以交易非常重要。 我使用@ControllerAdvice创建了一个ExceptionHandler来捕获业务层的所有异常 @ControllerAdvice public class HttpExceptionHandler { private String getStackTrace(final Throwable throwable) { final StringWriter stringWriter = new StringWriter()

我在一个投注网站上工作,所以交易非常重要。 我使用
@ControllerAdvice
创建了一个ExceptionHandler来捕获业务层的所有异常

@ControllerAdvice
public class HttpExceptionHandler {

  private String getStackTrace(final Throwable throwable) {
    final StringWriter stringWriter = new StringWriter()
    final PrintWriter printWriter = new PrintWriter(stringWriter, true)
    throwable.printStackTrace(printWriter)
    stringWriter.getBuffer().toString()
  }

  private List<String> filterStackTrace(final String stackTrace) {
    def stack = stackTrace.split('\n\t')
    stack.findAll({ it.contains('com.dsindigo.trading') })
  }

  private ResponseEntity<HttpErrorResponse> buildResponse(final Exception ex, final String message) {
    HttpErrorResponse error = new HttpErrorResponse(
      stack: filterStackTrace(getStackTrace(ex)),
      message: message
    )

    new ResponseEntity<HttpErrorResponse>(error, HttpStatus.BAD_REQUEST)
  }

  @ExceptionHandler(UsernameAlreadyExistsException)
  ResponseEntity<HttpErrorResponse> catchUsernameAlreadyExists(final UsernameAlreadyExistsException ex) {
    buildResponse(ex, HttpErrorMessages.USERNAME_ALREADY_EXISTS)
  }

  @ExceptionHandler(EmailAlreadyExistsException)
  ResponseEntity<HttpErrorResponse> catchEmailAlreadyExists(final EmailAlreadyExistsException ex) {
    buildResponse(ex, HttpErrorMessages.EMAIL_ALREADY_EXISTS)
  }

  //More exceptions...

  @ExceptionHandler(Exception)
  ResponseEntity<HttpErrorResponse> catchAny(final Exception ex) {
    buildResponse(ex, HttpErrorMessages.UNKNOWN)
  }
}
问题是,当我抛出一个没有
@Transactional
的自定义异常时,异常处理程序执行正确的方法,但是添加
@Transactional
总是执行常规
异常
方法

我遗漏了什么吗?

这些声明:

@ExceptionHandler(UsernameAlreadyExistsException)
应该是这样的:

@ExceptionHandler(UsernameAlreadyExistsException.class)

另外,请确保您的异常正在扩展RuntimeException。如果在代码中的任何其他地方(包括Spring的拦截器!)捕获并处理了异常,则ControllerAdvice将不会处理该异常。

这些是运行时异常还是已检查的异常?您确切地得到了哪种异常(例如,记录异常或使用调试器查找)?
@ExceptionHandler(UsernameAlreadyExistsException.class)
@ExceptionHandler(usernamealreadyexistceception.class)
你忘了
.class
异常的实际类是什么?从RuntimeException扩展就像一个charmI在使用Groovy一样,抱歉我忘了提到它,呵呵。我的自定义异常是从Exception扩展而来的,我将尝试从RuntimeException扩展谢谢!从RuntimeException扩展就像一个符咒!
@ExceptionHandler(UsernameAlreadyExistsException.class)