Reputation: 55
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
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
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.
In case you GET requests -
In case you POST requests -
Upvotes: 1
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