Javascript 参数相同、返回类型不同的Spring RESTful-GET方法
我使用Javascript 参数相同、返回类型不同的Spring RESTful-GET方法,javascript,java,jquery,rest,spring-mvc,Javascript,Java,Jquery,Rest,Spring Mvc,我使用Java/Sprig MVC RESTful应用程序,客户端使用它。在相同的输入参数和不同的返回类型中,我有2个RESTful方法。方法如下: // this method should return the `String` @RequestMapping(value = "wallets/{currencyName}/{walletName}", method = RequestMethod.GET , produces = "text/html")
Java/Sprig MVC RESTful应用程序
,客户端使用它。在相同的输入参数和不同的返回类型中,我有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);
}
URL
来自RESTful
方法,我希望它返回Long
。在这种情况下,我没有什么问题
a。它是否会像相同的GET
请求可能返回字符串和Long
一样工作
b。数据
已经是字符串
还是长
,或者我需要在上面做些什么
显然,我可以像window.open(“/WalletClient/balance.html?”+“currencyName=“+selectedCurrency+”&“+”walletName=“+walletName”)那样编写它代码>。
但是,在这种情况下,currencyName
和walletName
将向用户公开,我更愿意将其隐藏在URL
中
更新
我将代码更改为容纳一个可选参数,以区分长
和字符串
/**
* 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);
}
}
现在是否正确?您可以更改方法并返回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);
}
}
@RequestMapping(…)
公众反应你的方法(…){
//业务逻辑
如果(某些条件){
返回新的响应属性(地址,HttpStatus.OK);
}否则如果(…){
返回新的ResponseEntity(walletId,HttpStatus.OK);
}
}
好主意。如何修改该方法,使其可以说,它通常返回Long
,除非我提供布尔值true
,在这种情况下,它将提供字符串
?是的,您可以添加一个额外的参数,允许选择返回类型或返回Long和String两个值(如果使用简单的DTO对象).我更新了问题并提供了代码。请看一下好吗?如果在控制器中使用“@PathVariable”,则需要更改客户端代码:var url=“/rest/wallets/”+selectedCurrency+“/”+walletName;太好了,我忘了这件事
var url = "/rest/wallets/?" + "currencyName=" + selectedCurrency + "&" + "walletName=" + walletName;
@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);
}
}