Home > Blockchain >  How to get values from generic parameter (not related to any class) in Spring boot
How to get values from generic parameter (not related to any class) in Spring boot

Time:10-22

I'm new with SB & trying to enhance my skill. I make a little API project, in which I've account numbers with account balance property, and goal of the project is to adding up amount in account balance property with corresponding account number.

I'm stuck at the point where I need to get value from amount variable that is actually not the part of any class. Anyone can change the title if you think that it doesn't in according with the subject. I mention hereunder classes, you can find my efforts which is useless as it seems messed up. I'd grateful if anyone can correct or give me the reference stack where the same question lies.

Account.java

    private Long id;
    private int accountNumber;
    private BigDecimal accountBalance;

AccountRepository

 public interface AccountDao extends CrudRepository<Account, Long> {
  Account findByAccountNumber(int accountNumber);
 }

AccountService

public interface AccountService {
void deposit(int accountNumber, String accountType, double amount);
} // these fields are not related to class field, I took this to send data to API

AccountServiceImpl

public void deposit(String accountType, int accountNumber, double amount) {
    if (accountType.equalsIgnoreCase("Permanent")) {
        Account account = accountRepository.findByAccountNumber(accountNumber);
        account.setAccountBalance(account.getAccountBalance().add(new 
        BigDecimal(amount)));
        accountDao.save(account);

AccountController

 @PostMapping("/deposit")
   public ResponseEntity<Object> depositPost(@RequestBody double amount, String accountType, 
   int accountNumber, Account account){
   accountService.deposit(accountType, accountNumber, 
   Double.parseDouble(String.valueOf(amount)));
   return ResponseEntity.ok("Record has been entered");

}

Error I receive is:

  2021-10-21 20:19:58.660  WARN 22892 --- [nio-8088-exec-1] 
  .w.s.m.s.DefaultHandlerExceptionResolver : Resolved 
  [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: 
  Cannot deserialize value of type `double` from Object value (token 
  `JsonToken.START_OBJECT`); nested exception is 
  com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of 
  type `double` from Object value (token `JsonToken.START_OBJECT`)
  at [Source: (PushbackInputStream); line: 1, column: 1]]

CodePudding user response:

The issue is your POST body. I am guessing you are sending similar to the following:

{
   "amount": 1000
}

This is a JSON object that can't be deserialized into a simple double parameter. You need to create a corresponding class as follows:

public class AccountDto {
   private double amount;

   // constructor, getters, setters
}

Now you need to use it in your Controller:

@PostMapping("/deposit")
public ResponseEntity<Object> depositPost(@RequestBody AccountDto accountDto, String accountType, int accountNumber, Account account){
    accountService.deposit(accountType, accountNumber, accountDto.getAmount());
    return ResponseEntity.ok("Record has been entered");
}
  • Related