在 JAX-RS 中抛出自定义异常
Throw Custom Exception in JAX-RS
我想抛出自定义异常,但出现如下错误:
Error processing request. org.jboss.resteasy.spi.UnhandledException: com.daksa.restmockva.exception.InsufficientBalanceException: Insufficient Balance ........... blah-blah.... Caused by: com.daksa.restmockva.exception.InsufficientBalanceException: Insufficient Balance
这是我的 TransferService.java :
public Transaction transfer(TransactionModel param) throws InsufficientBalanceException{
Transaction transaction = new Transaction();
transaction.setId(param.getId());
transaction.setSrcAccountId(param.getSrcAccountId());
transaction.setDstAccountId(param.getDstAccountId());
transaction.setAmount(param.getAmount());
transaction.setTransactionTimestamp(new Date());
transaction.setTransactionDate(new Date());
Account accountSrc = accountRepository.getAccountById(param.getSrcAccountId());
Account accountDst = accountRepository.getAccountById(param.getDstAccountId());
if (!accountSrc.getAllowNegativeBalance() && accountSrc.getBalance().compareTo(param.getAmount()) < 0) {
throw new InsufficientBalanceException("Insufficient Balance");
}
accountSrc.setBalance(accountSrc.getBalance().subtract(param.getAmount()));
accountDst.setBalance(accountDst.getBalance().add(param.getAmount()));
entityManager.merge(accountSrc);
entityManager.merge(accountDst);
entityManager.persist(transaction);
return transaction;
}
这是我的 TransferResources.java :
@Transactional
@POST
@Path("transfer")
public Transaction doTransfer(TransactionModel param) throws InsufficientBalanceException {
return transactionService.transfer(param);
}
这是我的 InsufficientBalanceException.java :
public class InsufficientBalanceException extends Exception {
public InsufficientBalanceException(String errorMessage) {
super(errorMessage);
}
}
谢谢。
我会说,您需要为 InsufficientBalanceException
实施 ExceptionMapper
我想抛出自定义异常,但出现如下错误:
Error processing request. org.jboss.resteasy.spi.UnhandledException: com.daksa.restmockva.exception.InsufficientBalanceException: Insufficient Balance ........... blah-blah.... Caused by: com.daksa.restmockva.exception.InsufficientBalanceException: Insufficient Balance
这是我的 TransferService.java :
public Transaction transfer(TransactionModel param) throws InsufficientBalanceException{
Transaction transaction = new Transaction();
transaction.setId(param.getId());
transaction.setSrcAccountId(param.getSrcAccountId());
transaction.setDstAccountId(param.getDstAccountId());
transaction.setAmount(param.getAmount());
transaction.setTransactionTimestamp(new Date());
transaction.setTransactionDate(new Date());
Account accountSrc = accountRepository.getAccountById(param.getSrcAccountId());
Account accountDst = accountRepository.getAccountById(param.getDstAccountId());
if (!accountSrc.getAllowNegativeBalance() && accountSrc.getBalance().compareTo(param.getAmount()) < 0) {
throw new InsufficientBalanceException("Insufficient Balance");
}
accountSrc.setBalance(accountSrc.getBalance().subtract(param.getAmount()));
accountDst.setBalance(accountDst.getBalance().add(param.getAmount()));
entityManager.merge(accountSrc);
entityManager.merge(accountDst);
entityManager.persist(transaction);
return transaction;
}
这是我的 TransferResources.java :
@Transactional
@POST
@Path("transfer")
public Transaction doTransfer(TransactionModel param) throws InsufficientBalanceException {
return transactionService.transfer(param);
}
这是我的 InsufficientBalanceException.java :
public class InsufficientBalanceException extends Exception {
public InsufficientBalanceException(String errorMessage) {
super(errorMessage);
}
}
谢谢。
我会说,您需要为 InsufficientBalanceException
实施 ExceptionMapper