簡體   English   中英

如何在 spring mvc 中使用 @PathParam 傳遞 2 個或更多變量? 並假設如果我想使用 postman 對其進行測試該怎么做?

[英]How to pass 2 or more variables using @PathParam in spring mvc? and suppose if I want to test it out using postman how to do that?

我正在嘗試使用 JPA 存儲庫方法從數據庫中獲取值

product findByIdNumberOrCifNumber(String idNumber, String cifNumber);

服務 class 邏輯:-

public ResponseModel FindByCivIDOrCifNumber(String idNumber,String cifNumber) {
    ResponseModel responseModel = new ResponseModel();
    Optional<product> civId = Optional.ofNullable(productRepos.findByIdNumber(idNumber));
    if (civId.isPresent()) {
        responseModel.setResponse(productRepos.findByIdNumberOrCifNumber(idNumber,cifNumber));
    } else {
        errorModel errorModel1 = new errorModel();
enter image description here        errorModel1.setErrorCode(productConstant.INVALID_REQUEST);
        errorModel1.setErrorDescription("Requested Civil Id or CifNUmber is not present");
        responseModel.setErrorModel(errorModel1);
    }
    return responseModel;

}




 

controller class:-

@GetMapping("/getByCifNoOrGetByIdNo")
    public ResponseModel getProductByCifNoOrGetByIdNo(@RequestParam String idNumber,@RequestParam String cifNumber )  {
        return productService.FindByCivIDOrCifNumber(idNumber,cifNumber);
    }

郵遞員:-

請幫我弄清楚如何讓它工作:)

如果您正在尋找傳遞兩個或更多路徑變量並使用 postman 進行測試的答案,您可以試試這個。

@GetMapping("/api/mapping-name/{variable1}/{variable2}")
   

在這里您將獲得兩個路徑變量,您可以通過語法訪問它們

@PathVariable("variable1") datatype variableName 

現在在 postman 請求 url 你可以簡單地給出相應的 url,比方說:

https://localhost8080/api/mapping-name/:variable1/:variable2

這會自動在參數的路徑變量部分中為您提供一個鍵值部分,並根據您提供的名稱預先填充鍵名稱。 在這種情況下,變量 1 和變量 2。

給出相應的值,它應該可以工作。

暫無
暫無

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

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