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); }
解决方法:在路径参数后面添加/
@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有“/”也行,不过为了统一,最好前后端都写吧
前端调用的时候也要加上“/”
tableOption.ajax = cacService.assembleDataTableUrl('/api/cac/audit/metrics/jobId/' + jobId + '/' + hostKey + '/');