0

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]]
Seeschon
  • 408
  • 1
  • 9
  • 20
  • Maybe [trying-to-use-spring-boot-rest-to-read-json-string-from-post](https://stackoverflow.com/questions/29313687/trying-to-use-spring-boot-rest-to-read-json-string-from-post) can help you. – dariosicily Oct 21 '21 at 20:52
  • Thanks, but i'm more interested to know as to how to get values from variable. for example, variable amount parses 100 & I need to add it into accountBalance property – Seeschon Oct 21 '21 at 22:38
  • You are welcome, the message you receive is about a parsing json error,could you add a json example message you send to the controller ? – dariosicily Oct 22 '21 at 05:54

1 Answers1

1

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");
}
João Dias
  • 16,277
  • 6
  • 33
  • 45
  • Thanks Joäo, I get an error "java.lang.IllegalStateException: Optional int parameter 'accountNumber' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type"...... – Seeschon Oct 22 '21 at 00:51
  • 1
    If `accountNumber` may be null, you need to use `Integer` instead of `int`. – João Dias Oct 22 '21 at 08:59
  • Thank Joäo, it works but do you have solution on other way ? because I send three values from Postman accountNumber, accountType & amount. So I had to change controller like this, just tell me if this is the normal way ?:::::: public ResponseEntity depositPost(@RequestBody AccountDto accountDto){ accountService.deposit(accountDto.getAccountType(), accountDto.getAccountNumber(), accountDto.getAmount()); – Seeschon Oct 22 '21 at 09:39
  • 1
    Yes, that is the right way to do it. – João Dias Oct 22 '21 at 09:40