我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,
如果點擊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
。我在這種情況下有幾個問題,
a。它會工作,因爲相同的GET
請求可能會返回String
和Long
?
b。是data
已經是String
或Long
或者我需要對它做些什麼?
Obvously,我可以像window.open("/WalletClient/balance.html?" + "currencyName=" + selectedCurrency + "&" + "walletName=" + walletName);
寫。 然而,在這種情況下,currencyName
和walletName
將暴露給用戶,我會更喜歡隱藏在URL
。
UPDATE
我改變的代碼accomodatean可選參數不同的Long
和String
之間,
/**
* 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;
是這現在正確嗎?
好主意。我如何修改方法說,它通常會返回'長',除非我提供'布爾真',在這種情況下,它會提供'字符串'? – Arefe
是的,您可以添加一個額外的參數,允許選擇返回類型或返回值Long和String(如果您使用簡單的DTO對象)。 – Shchipunov
我更新了問題並提供了代碼。你能看一下嗎? – Arefe