[英]Request method 'GET' not supported with 'POST' mapping in Spring boot
您好,我正在嘗試創建一個 POST 方法,但我不斷收到“404 請求方法 'GET' 不支持”錯誤。 下面我將發布我的 Rest controller 和下面我將發布我的服務 class。 唯一不起作用的是@PostMapping 方法。
@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;
}
}
我沒有看到問題,我嘗試切換到@GetMapping 並刪除了實際事務“billRepository.delete(billToWithdraw);” 然后該方法返回正確的賬單。
正如錯誤所說404 Request method 'GET' not supported
意味着您正在發出 GET 請求而不是 POST。
您可以使用Postman
之類的工具進行發布請求。 通過任何瀏覽器點擊/{id}/withdraw/{amount}
將提示 GET 請求而不是 POST 請求。
問題是您正在向配置為僅接受POST
請求的端點發送GET
請求。 這可能會幫助您測試它們。
如果您 GET 請求 -
如果您發布請求 -
就我而言,問題是我調用了 https://localhost:8080/my-service 但端口 8080 不支持 HTTPS 所以我將調用更改為 Z80791B3AE7002CB88C246870D09 並解決了 my88C246870D8Z9 問題。 但是,當使用 https spring 調用 http 時,會在內部發出 GET 請求
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.