[英]How to correctly escape '/' in Spring Rest @PathVariable
在Spring Boot 1.5.4中,我有一個如下的請求映射:
@RequestMapping(value = "/graph/{graphId}/details/{iri:.+}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public JSONObject getGraph(@PathVariable Long graphId,
@PathVariable String iri) {
log.debug("Details called for graph ID {} for IRI {}", graphId, iri);
return detailsService.getDetails(graphId, iri);
}
訪問
HTTP://本地主機:9000 / API / V1 /圖形/ 2 /信息/ HTTP%3Anthnth33
工作正常,服務器正確映射了請求,代碼返回了預期的結果
但是訪問
給出錯誤的服務器請求 (無法加載資源:服務器以400(錯誤請求)的狀態響應)。 在這種情況下,甚至沒有完成映射到端點的請求。
顯然,編碼為%2F的斜杠“ /”(使用encodeURIComponent())會引起麻煩。 為什么? 我想念什么? uri參數應該如何編碼?
問題不僅在於如何提取PathVariables,還在於如何強制String識別正確的映射。
您的示例的問題是Spring如何進行路徑匹配。 您提供的URL作為示例
http://localhost:9000/api/v1/graph/2/details/http%3A%2F%2Fserverurl.net%2Fv1%2Fus%2Fh.schumacher%408tsch.net%2Fn%2FLouSchumacher
將被容器解碼為
在由Spring Matcher處理之前。 這使得matche認為這只是http:
對應{iri:.+}
和后來去/
所以這是你沒有一個映射一些較長的路徑。
這里描述的方法應該對您有用:Spring 3 RequestMapping:獲取路徑值
@RequestMapping(value = "/graph/{graphId}/details/**",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public JSONObject getGraph(@PathVariable Long graphId,
HttpServletRequest request) {
String iri = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
log.debug("Details called for graph ID {} for IRI {}", graphId, iri);
return detailsService.getDetails(graphId, iri);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.