Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 参数相同、返回类型不同的Spring RESTful-GET方法_Javascript_Java_Jquery_Rest_Spring Mvc - Fatal编程技术网

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