如何读取从请求

问题描述:

我想用弹簧读取请求URL的URL,我有方法如下面和所述客户机请求的URL是像http://localhost:8080/api/getName,我想(从该URL/API /的getName)读如何读取从请求

@Controller 
public class TestController { 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public ResponseEntity<String> getDetails(
      final HttpServletRequest request, 
      final HttpServletResponse response) throws Exception { 

     } 
+0

为什么?你打算把自己的调度机制放在已经在Spring中的调度机制之上吗? – 2012-02-21 17:05:25

这是HttpServletRequest中的一种方法:request.getRequestURL()为您提供网址。

的一些细节看到这个答案:How to get the request url from HttpServletRequest

为了进一步分析URL,使用它的方法:Parsing a URL

+0

有没有什么方法可以从整个url中读取'/ api/getName':http:// localhost:8080/api/getName? – user1195292 2012-02-21 18:10:30

+0

在URL上使用'getPath()':http://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html – 2012-02-22 08:04:44

+0

@ user1195292:方法getRequestURI()正好返回您要求的内容。请参阅我的答案中链接的API文档。 – 2012-02-22 10:46:01

req.getContextPath();

应该给你你正在寻找的上下文路径。

我想(从这个网址/ API /的getName)读

为此,您可以使用HttpServletRequest的方法getRequestURI()。它将返回请求URL,不包含协议/服务器/端口部分,也不返回查询字符串(参数)。