我与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,
如果点击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
请求可能会返回String
和Long
?
湾是data
已经是String
还是Long
还是我需要对其进行操作?
很明显,我可以像window.open("/WalletClient/balance.html?" + "currencyName=" + selectedCurrency + "&" + "walletName=" + walletName);
那样写。
但是,在这种情况下,currencyName
和walletName
会向用户展示,我更愿意将其隐藏在URL
中。
UPDATE
我更改了代码以适应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;
现在这是正确的吗?
答案 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);
}
}