我已经在线搜索并尝试了一些建议的更正,但我的应用无法运行。它不为类型异常以及UnsatisfiedDependencyException抛出任何属性:创建bean时出错。
[尝试运行代码时,它说出原因:java.lang.IllegalArgumentException:无法为方法public abstract java.math.BigDecimal com.Wallet.Repository.TransactionRepository.getBalance(java.lang.Long)创建查询!找不到交易类型的属性getBalance
这是型号代码:
@Entity
public class Transaction {
@Id
@GeneratedValue
private Long id;
private BigDecimal amount;
private java.util.Date transactionDate;
private Long transactionReference;
private String details;
@ManyToOne
private UserWallet wallet;
public Transaction() {
super();
}
存储库
@Repository
public interface TransactionRepository extends CrudRepository <Transaction, Long>
{
Optional<Transaction> getTransactionByRef(Long reference)throws UserNotFoundException;
BigDecimal balanceByUserAccountID(Long accountId);
List<Transaction> transactionsByUserAccountID(Long Id)throws UserNotFoundException;
Transaction createTransaction(Transaction transaction) throws LowBalanceException;
BigDecimal getBalance(Long id);
}
ServiceImpl
@Service
public class TransactionServiceImpl {
@Autowired
private TransactionRepository transRepo;
public Object transactionByRef(Long reference) throws UserNotFoundException {
return transRepo.getTransactionByRef(reference).orElseThrow(
() -> new UserNotFoundException(String.format("transaction with ref '%d' doesnt exist", reference)));
}
@Transactional
public Transaction createTransaction(Transaction transaction) throws LowBalanceException {
BigDecimal balance = transRepo.getBalance(transaction.getWallet().getId());
if (balance.add(transaction.getAmount()).compareTo(BigDecimal.ZERO) >= 0) {
return transRepo.save(transaction);
}
throw new LowBalanceException(String.format("user's balance is %.2f and cannot perform a transaction of %.2f ",
balance.doubleValue(), transaction.getAmount().doubleValue()));
}
public BigDecimal balanceByUserAccountID(Long accountId) {
return transRepo.getBalance(accountId);
}
public Iterable<Transaction> getList() {
return transRepo.findAll();
}
public Optional<Transaction> transactionsByUserAccountID(Long txnRef) throws UserNotFoundException {
return transRepo.getTransactionByRef(txnRef);
}
public Iterable<Transaction> transactions() {
return transRepo.findAll();
}
}
Controller
@RestController
@RequestMapping("v1/addMoney")
public class TransactionController {
@Autowired
private UserAccountServiceImp userRepo;
@Autowired
private TransactionServiceImpl transactRepo;
@PostMapping("/{id}")
public ResponseEntity addMoney(@PathVariable("id") Long userAccountId, @RequestBody TransactionDTO walletDTO) {
Transaction saved;
try {
walletDTO.setUserAccountId(userAccountId);
saved = transactRepo.createTransaction(TransactionMapper.dtoToDO(walletDTO));
} catch (LowBalanceException ex) {
Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
return new ResponseEntity<String>(ex.getMessage(), HttpStatus.BAD_REQUEST);
} catch (Exception ex) {
Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
return new ResponseEntity<String>(ex.getMessage(), HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<TransactionDTO>(TransactionMapper.doToDTO(saved), HttpStatus.CREATED);
}
@GetMapping("v1/balance/{id}")
public BigDecimal getBalance(@PathVariable Long userAccountId){
return transactRepo.balanceByUserAccountID(userAccountId);
}
}
balance
的字段,您就应该很好:@Entity
public class Transaction {
@Id
@GeneratedValue
private Long id;
private BigDecimal amount;
private java.util.Date transactionDate;
private Long transactionReference;
private String details;
private BigDecimal balance;
@ManyToOne
private UserWallet wallet;
public Transaction() {
super();
}