2017-08-16 24 views
0

Java/ Sprig MVC RESTful app工作和客戶消費它。我有2 RESTful方法在相同的輸入參數和不同的返回類型。提供的方法如下,春天的RESTful GET方法具有相同的參數和不同的返回類型

// 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按鈕,我想打開一個新頁面URLhttp://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。我在這種情況下有幾個問題,

a。它會工作,因爲相同的GET請求可能會返回StringLong

b。是data已經是StringLong或者我需要對它做些什麼?

Obvously,我可以像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; 

是這現在正確嗎?

回答

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); 
    } 
} 
+0

好主意。我如何修改方法說,它通常會返回'長',除非我提供'布爾真',在這種情況下,它會提供'字符串'? – Arefe

+1

是的,您可以添加一個額外的參數,允許選擇返回類型或返回值Long和String(如果您使用簡單的DTO對象)。 – Shchipunov

+0

我更新了問題並提供了代碼。你能看一下嗎? – Arefe

相關問題