0

I'm new to restful web services and so far all my get and post requests are working fine. I'm trying to update a user's account with a PUT request but it's giving a 415 error. I've checked online and it seems that it could be a simple solution but I just can't seem to get one that works.

I have a Customer class with a few attributes, but the important one here is an ArrayList of Accounts (called 'account').

Account has a sortCode, accountNumber, (int) balance, and an accountId (long)

The PUT request looks like this:

@PUT
@Path("/{customerName}/account/{accountId}")
public Account updateAccountBalance(@PathParam("customerName") String customerName, @PathParam("accountId") int accountId, int amount) {
    return customerService.updateAccountBalance(customerService.getCustomer(customerName), accountId, amount);
}

It's calling the customerService.updateAccountBalance method which looks like:

public Account updateAccountBalance(Customer customer, int accountId, int amount){
    customer.getAccountById(accountId).setBalance((customer.getAccountById(accountId).getBalance())+amount);
    customers.put(customer.getName(), customer);
    return customer.getAccountById(accountId);
}

The amount was originally a String but now I've changed it to an int as it needs to be updated by the user

I've tried a few different calls on Postman like so:

{
"balance": 12
}

{
"accountNumber": "12345678",
"balance": 12,
"sortCode": "123456"
}

The aim here is to add 12 to the existing account balance. The URL I'm using is http://localhost:8080/api/customer/test/account/

The GET works to get the account but I'm not sure why the PUT won't

The GET is:

@GET
@Path("/{customerName}/account/{accountId}")
public Account getCustomerIndividualAccount(@PathParam("customerName") String customerName, @PathParam("accountId") int accountId) {
    return customerService.getAccountById(customerService.getCustomer(customerName), accountId-1);
}

getAccountById looks like this:

public Account getAccountById(Customer customer, int accountId){
    return customer.getAccountById(accountId);       
}

and will return this:

{
"accountId": 1,
"accountNumber": "12345678",
"balance": 1234,
"sortCode": "123456"
}

Sorry for the long post but any help would be appreciated. Also to note, I have the Content-Type as application/json in the request

If you need any more information, let me know.

achAmháin
  • 4,176
  • 4
  • 17
  • 40

0 Answers0