[英]How to inject the full request path in Spring @RestController?
如何在 Spring @RestController
中注入完整的子路徑?
在我的示例中,我希望subpaths=/test/sub/path
@RestController
public class MyController {
@GetMapping(value = "/v1/{subpaths}", produces = MediaType.APPLICATION_JSON)
public Object handle(@PathVariable String subpaths) {
//this is not called for /v1/test/sub/path
}
}
實際上,如果想法是有一個入口點,我們可以捕獲所有內容,然后提取子路徑。 以下說明相同:
@RestController
public class MyController {
@GetMapping(value = "/v1/parentPath/**", produces = MediaType.APPLICATION_JSON)
public Object handle(@RequestParam(name = "myParam") String myParam,HttpServletRequest request) {
//Get the complete url
String url=request.getRequestURL();
String[] paths=url.split("parentPath/");
//paths[1] is the desired subPath
//Provides URI
// example : /context-path/v1/parentPath/subPath
String uri=request.getRequestURI();
//Returns query string, ex: test=abc&page=1
String queryString=request.getQueryString();
//Returns query parameters as map,Includes both query and ported form data
Map<String, String[]> parameterMap=request.getParameterMap();
}
}
HttpServletRequest :由 Spring 自動注入。(將嘗試查找一些文檔並在此處添加)
為了安全起見,添加了 parentPath 以確保我們不會捕獲 v1 下的所有請求
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.