Spring RESTful GET方法,具有相同的参数和不同的返回类型

时间:2017-08-16 12:37:22

标签: javascript java jquery rest spring-mvc

我与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);
    } 

在客户端,我有这样的UI,

enter image description here

如果点击Balance按钮,我想打开一个URL 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已经是String还是Long还是我需要对其进行操作?

很明显,我可以像window.open("/WalletClient/balance.html?" + "currencyName=" + selectedCurrency + "&" + "walletName=" + walletName);那样写。 但是,在这种情况下,currencyNamewalletName会向用户展示,我更愿意将其隐藏在URL中。

UPDATE

我更改了代码以适应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;

现在这是正确的吗?

1 个答案:

答案 0 :(得分:1)

您可以更改方法并返回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);
    }
}
相关问题