簡體   English   中英

我不想在 API 端點中將數組作為請求參數傳遞時刪除尾隨空格。 我該怎么做?

[英]I don't want to remove trailing spaces while passing an array as request param in API endpoint. How should I do that?

我正在 angular 中創建一個字符串數組,並將該數組作為請求參數發送到 API 端點。 下面是代碼。

但是,當列表在 java API 端點中傳遞時,尾隨空格被刪除,我不想刪除它。 例如,如果列表是 ['hello', 'world'],那么我想將它按原樣傳遞給 API。 而不是在下面的代碼中,它被傳遞為 ['hello','world']

任何人都可以幫助我嗎?

Angular:

    const lst: any=[];
      for(let i=0;i<data.length;i++){
        lst.push(data[i][0]);
      }
 xlst=await this.getdatafromAPI(lst);

getdatafromAPI(value:[])
  {
    const header = this.servicename.getHeader();
    this.servicename.addForwardUrlInHeader(header, this.URL+ '?param=' + value );
    return fetch(
      this.URL,
      header
    ).then(response =>
    {
      if (response.status === 200)
      {
        return response.json();
      } else
      {
        console.log('Unable To Get lst');
      }
    });
  }

API 端點

    @GetMapping(value="/api/xyz")
    public ResponseEntity<List<abc>> getDatafromDb(@RequestParam List<String> lst){

        List<abc> newarrlst=new ArrayList<>();
        try
        {
            for(int i=0;i<lst.size();i++){
                abc obj=new abc();
                String propvalue=lst.get(i);
                obj.setproperty1(propvalue);
                obj.setproperty1(map.get(propvalue)); // I already have a map from where I am getting values
                newarrlst.add(obj);
            }

您需要對value內容進行編碼。

這可以通過 encodeURIComponent 來完成:

這將使用轉義序列對空格和其他特殊字符進行編碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM