[英]How to read the url from request
我想使用spring讀取請求url,我有下面的方法,客戶端請求url如http:// localhost:8080 / api / getName ,我想讀取(/ api / getName from this url)
@Controller
public class TestController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public ResponseEntity<String> getDetails(
final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
}
這是HttpServletRequest
的方法: request.getRequestURL()
為您提供URL。
有關更多詳細信息,請參見此答案: 如何從HttpServletRequest獲取請求url。
要進一步分析URL,請使用其方法: 解析URL
req.getContextPath();
應該為您提供所需的上下文路徑。
您可能還想瀏覽api javadocs
http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html
我想閱讀(/ api / getName from this url)
為此,您可以使用HttpServletRequest方法getRequestURI() 。 它將返回請求URL,不帶協議/服務器/端口部分,也不帶查詢字符串(參數)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.