簡體   English   中英

Spring 引導中的“POST”映射不支持請求方法“GET”

[英]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 請求 -

  1. 您可以直接從瀏覽器地址欄中查看 api。 輸入 api 並回車。就這么簡單!
  2. 您可以使用 Postman、SoapUI 等工具發送 GET 請求。
  3. 您可以使用 action="get mapping uri" 和 method="GET" 編寫 html 表單
  4. 如果您的 API 使用任何文檔或設計工具,例如 swagger,您可以從其界面對其進行測試。

如果您發布請求 -

  1. 您不能直接從瀏覽器地址欄中查看 api。
  2. 您可以使用 Postman、SoapUI 等工具發送 POST 請求。
  3. 您可以使用 action="post mapping uri" 和 method="POST" 編寫 html 表單。
  4. 如果您的 API 使用任何文檔或設計工具,例如 swagger,您可以從其界面對其進行測試。

就我而言,問題是我調用了 https://localhost:8080/my-service 但端口 8080 不支持 HTTPS 所以我將調用更改為 Z80791B3AE7002CB88C246870D09 並解決了 my88C246870D8Z9 問題。 但是,當使用 https spring 調用 http 時,會在內部發出 GET 請求

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM