ValleTSF
ValleTSF

Reputation: 55

Request method 'GET' not supported with 'POST' mapping in Spring boot

Hello I'm trying to create a POST method and I keep getting the "404 Request method 'GET' not supported" error. Below I'll post my Rest controller and below that I'll post my service class. The only thing not working is the @PostMapping method.

@RequestMapping("/ATM")
public class ATMController {

    private ATMService atmService;

    @Autowired
    public ATMController(ATMService atmService) {
        this.atmService = atmService;
    }

    @GetMapping(path = "/{id}")
    public ATM getATMById(@PathVariable long id){
        return atmService.getByID(id);
    }

    @PostMapping(path = "/{id}/withdraw/{amount}")
    public List<Bill> withdrawMoney(@PathVariable long id,@PathVariable float amount){
       return atmService.withdrawMoney(id,amount);
    }
}
@Service
public class ATMService {

    private ATMRepository atmRepository;
    private BillRepository billRepository;

    @Autowired
    public ATMService(ATMRepository atmRepository, BillRepository billRepository) {
        this.atmRepository = atmRepository;
        this.billRepository = billRepository;
    }

    public void save(ATM atm) {
        atmRepository.save(atm);
    }

    public ATM getByID(Long id) {
        return atmRepository.findById(id).get();
    }

    public List<Bill> getBillList(Long id) {
        return atmRepository.findById(id).get().getBillList();
    }

    @Transactional
    public List<Bill> withdrawMoney(Long id, float amount) {
        List<Bill> allBills = getBillList(id);
        List<Bill> billsToWithdraw = new ArrayList<>();
        float amountTransferred = 0;

        for (Bill bill : allBills) {
            if (bill.getValue() == 100) {
                billsToWithdraw.add(bill);
                amountTransferred += bill.getValue();
            }
            if (amountTransferred == amount) {
                for (Bill billToWithdraw : billsToWithdraw) {
                    billRepository.delete(billToWithdraw);
                }
                return billsToWithdraw;
            }
        }
        return null;
    }
}

I don't see the issue, I've tried switching to @GetMapping and removed the actual transaction "billRepository.delete(billToWithdraw);" and the method then returns the correct bills.

Upvotes: 0

Views: 13442

Answers (3)

Anil
Anil

Reputation: 1

In my case the problem was that I called https://localhost:8080/my-service but the port 8080 not supports HTTPS so I changed my call to http://localhost:8080 and resolved my problem. However when calling a http with https spring makes internally a GET Request

Upvotes: 0

Mukesh Keshu
Mukesh Keshu

Reputation: 467

The issue is that you are sending a GET request to an end point that is configured to accept only POST request. This will probably help you to test them.

How to test

In case you GET requests -

  1. You CAN directly check the api from the browser address bar. Type in the api and hit enter.Its that Simple!
  2. You can use a tool such as Postman, SoapUI, etc to send a GET request.
  3. You could write an html form with action="get mapping uri" and method="GET"
  4. If your API uses any documentation or design tools such as swagger you can test it from its interface.

In case you POST requests -

  1. You CANNOT directly check the api from the browser address bar.
  2. You can use a tool such as Postman, SoapUI to send a POST request.
  3. You could write an html form with action="post mapping uri" and method="POST".
  4. If your API uses any documentation or design tools such as swagger you can test it from its interface.

Upvotes: 1

Shreyas B
Shreyas B

Reputation: 505

As the error says 404 Request method 'GET' not supported means you are making a GET request instead of POST.

You can make use of tools like Postman to make a post request. Hitting /{id}/withdraw/{amount} via any browser will prompt a GET request and not a POST request.

Upvotes: 1

Related Questions