springmvc傳遞有特殊字符的路徑參數

由於hostKey這裏是IP(例如127.0.0.1)包含了特殊字符.  實際傳遞到後臺的是127.0.0少了一截前端

@GetMapping("/metrics/jobId/{jobId}/{hostKey}") @ResponseBody public List<AuditMetric> getAuditMetricByJobId(@PathVariable String jobId,@PathVariable String hostKey) throws Exception { log.debug("REST request to get AuditJob By Ansible JobID : {}", jobId);
        return auditMetricRepository.findByJobIdAndHostKey(jobId,hostKey); }

 

解決方法:在路徑參數後面添加/ajax

@GetMapping("/metrics/jobId/{jobId}/{hostKey}/") @ResponseBody public List<AuditMetric> getAuditMetricByJobId(@PathVariable String jobId,@PathVariable String hostKey) throws Exception { log.debug("REST request to get AuditJob By Ansible JobID : {}", jobId);
        return auditMetricRepository.findByJobIdAndHostKey(jobId,hostKey); }

 

補充:後端

我發現後臺不用最後的「/」,只要前端的url有「/」也行,不過爲了統一,最好先後端都寫吧api

 

前端調用的時候也要加上「/」app

tableOption.ajax = cacService.assembleDataTableUrl('/api/cac/audit/metrics/jobId/' + jobId + '/' + hostKey + '/');
相關文章
相關標籤/搜索