簡體   English   中英

具有相同參數和不同返回類型的Spring RESTful GET方法

[英]Spring RESTful GET method with same parameters and different return type

我使用Java/ Sprig MVC RESTful app ,客戶端使用它。 我在相同的輸入參數和不同的返回類型中有2個RESTful方法。 下面提供了這些方法,

// this method should return the `String` 
@RequestMapping(value = "wallets/{currencyName}/{walletName}", method = RequestMethod.GET
            , produces = "text/html")
    public ResponseEntity<String> getAddressWithCurrencyAndWalletName(@PathVariable("currencyName") String currencyName,
                                                                      @PathVariable("walletName") String walletName) {

        logger.info("The currency name is {} and wallet name is {}", currencyName, walletName);
        WalletInfo walletInfo = walletService.getWalletInfoWithCurrencyAndWalletName(currencyName, walletName);

        if (Objects.isNull(walletInfo)) {
            return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
        }

        String address = walletInfo.getAddress();
        return new ResponseEntity<String>(address, HttpStatus.OK);
    }


    // this method should return the `Long` 
    @RequestMapping(value = "wallets/{currencyName}/{walletName}", method = RequestMethod.GET,
            produces = "text/html")
    public ResponseEntity<Long> getWalletIdWithCurrencyAndWalletName(@PathVariable("currencyName") String currencyName,
                                                                     @PathVariable("walletName") String walletName) {

        logger.info("The currency name is {} and wallet name is {}", currencyName, walletName);
        WalletInfo walletInfo = walletService.getWalletInfoWithCurrencyAndWalletName(currencyName, walletName);

        if (Objects.isNull(walletInfo)) {
            return new ResponseEntity<Long>(HttpStatus.NOT_FOUND);
        }

        Long walletId = walletInfo.getId();
        return new ResponseEntity<Long>(walletId, HttpStatus.OK);
    } 

在客戶端,我有這樣的用戶界面,

在此處輸入圖片說明

如果單擊“ Balance按鈕,我想打開一個URLhttp://localhost:63342/WalletClient/balance.html?walletId=someValue的新頁面,並且我想為此使用http://localhost:63342/WalletClient/balance.html?walletId=someValue RESTful方法。 我想象客戶端代碼是這樣的;

$(document).ready(function () {

    var walletName, selectedCurrency;

    // generic request function with the URL, method name and
    // the request (GET, POST, PUT, DELETE etc) data
    function request(url, method, data) {
        $.ajax({
            url: baseUrl + url,
            // url: url,
            method: method,
            data: data
        })
    }

    // some code 
    // we have the walletName and selectedCurrency values extracted 

    $("#balance").click(function () {

            console.log("Open the balance page");

            var url = "/rest/wallets/?" + "currencyName=" + selectedCurrency + "&" + "walletName=" + walletName;

            // get the wallet Id from the cureny name and the wallet name
            request(url, "GET").done(function (data) {
                window.open("/WalletClient/balance.html?walletId=" + data);
            });
        });
}

URL來自RESTful方法,我希望它返回Long 在這種情況下,我有幾個問題,

一種。 它會像同一個GET請求一樣可能返回StringLong嗎?

data已經是StringLong還是我需要對它做點什么?

顯然,我可以這樣寫: window.open("/WalletClient/balance.html?" + "currencyName=" + selectedCurrency + "&" + "walletName=" + walletName); 但是,在這種情況下, currencyNamewalletName將向用戶公開,我更希望將其隱藏在URL

UPDATE

我將代碼更改為accomodatean可選參數,以區分LongString

 /**
     * get the wallet address with the currency name and the wallet name
     * 
     * returns the Long value for the walletInfo 
     * curl -i -H "Accept: text/html" http://localhost:8080/rest/wallets/bitcoin/puut | json
     *
     * 
     * returns the String value for the walletInfo address 
     * curl -i -H "Accept: text/html" http://localhost:8080/rest/wallets/bitcoin/puut/true | json
     * 
     * @param currencyName
     * @param walletName
     * @return
     */
    @RequestMapping(value = "wallets/{currencyName}/{walletName}", method = RequestMethod.GET
            , produces = "text/html")
    public ResponseEntity<?> getAddressWithCurrencyAndWalletName(@PathVariable("currencyName") String currencyName,
                                                                 @PathVariable("walletName") String walletName
            , @RequestParam(value = "address", required = false) boolean address) {

        logger.info("The currency name is {} and wallet name is {}", currencyName, walletName);
        WalletInfo walletInfo = walletService.getWalletInfoWithCurrencyAndWalletName(currencyName, walletName);

        if (Objects.isNull(walletInfo)) {
            return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
        }

        // address values is expected 
        if(address){

            String addressValue = walletInfo.getAddress();
            return new ResponseEntity<String>(addressValue, HttpStatus.OK);
        }

        else {
            Long walletId = walletInfo.getId();
            return new ResponseEntity<Long>(walletId, HttpStatus.OK);
        }
    }

客戶端URL將是這樣,

var url = "/rest/wallets/?" + "currencyName=" + selectedCurrency + "&" + "walletName=" + walletName;

現在正確嗎?

您可以更改方法並返回ResponseEntity<?>類型。 這將是:

@RequestMapping(...)
public ResponseEntity<?> yourMethod(...) {
    // business-logic
    if (some condition) {
        return new ResponseEntity<String>(address, HttpStatus.OK);
    } else if (...) {
        return new ResponseEntity<Long>(walletId, HttpStatus.OK);
    }
}

暫無
暫無

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

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