簡體   English   中英

Spring Boot @ControllerAdvice + @Transactional 沒有按預期工作

[英]Spring Boot @ControllerAdvice + @Transactional not working as expected

我在一個投注網站上工作,所以交易非常重要。 我使用@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)
  }
}

所以基本上它會捕獲一個異常(例如UsernameAlreadyExistsException )並創建一個包含自定義消息和堆棧跟蹤(用於調試目的)的 JSON 響應。

這是拋出自定義異常的服務示例:

@Service
class UserServiceImpl implements UserService {

  // @Autowired stuff ...

  @Override
  @Transactional
  UserDTO save(UserDTO user) {
    UserDTO current = findOneByUsername(user.username)

    if (current != null)
      throw new UsernameAlreadyExistsException()

    current = findOneByEmail(user.email)

    if (current != null)
      throw new EmailAlreadyExistsException()

    ConfigurationDTO configuration = configurationService.findOne();

    user.active = false
    user.password = bCryptPasswordEncoder.encode(user.password)
    user.balance = configuration.initialBalance

    User entity = mapper.map(user, User)
    entity = userRepository.save(entity)
    user = mapper.map(entity, UserDTO)

    transactionService.createForUser(user.id, INITIAL_CHIPS_ID, configuration.initialBalance)

    TokenDTO token = tokenService.createForUser(user.id)
    emailService.sendRegisterMessage(user.email, token.token)

    user
  }
}

問題是,當我在沒有@Transactional的情況下拋出自定義異常時,異常處理程序執行正確的方法,但添加@Transactional總是執行一般的Exception方法。

我錯過了什么嗎?

這些聲明:

@ExceptionHandler(UsernameAlreadyExistsException)

應該是這樣的:

@ExceptionHandler(UsernameAlreadyExistsException.class)

另外,請確保您的異常擴展了 RuntimeException。 如果異常在代碼中的任何其他地方被捕獲和處理(包括 Spring 的攔截器!),它不會被 ControllerAdvice 處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM