0

I want to read a request url using spring ,i have method like below and the client request url is like http://localhost:8080/api/getName ,i want to read (/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 {

        }
Raghuram
  • 51,854
  • 11
  • 110
  • 122
user1195292
  • 233
  • 1
  • 3
  • 13

4 Answers4

1

It's a method in HttpServletRequest: request.getRequestURL() gives you the URL.

See this answer for some more details: How to get the request url from HttpServletRequest

To further analyse the URL, use it's methods: Parsing a URL

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

req.getContextPath();

Should get you the context path you are looking for.

ring bearer
  • 20,383
  • 7
  • 59
  • 72
0

You might also want to browse the api javadocs

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

bluesman
  • 2,242
  • 2
  • 25
  • 35
0

i want to read (/api/getName from this url)

For this you can use the HttpServletRequest method getRequestURI(). It will return the request URL, without the protocol/server/port part, and without the query String (the parameters).

Tomas Narros
  • 13,390
  • 2
  • 40
  • 56