user1195292
user1195292

Reputation: 233

How to read the url from request

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 {

        }

Upvotes: 0

Views: 693

Answers (4)

Aaron Digulla
Aaron Digulla

Reputation: 328604

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

Upvotes: 1

Tomas Narros
Tomas Narros

Reputation: 13468

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).

Upvotes: 0

bluesman
bluesman

Reputation: 2260

You might also want to browse the api javadocs

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

Upvotes: 0

ring bearer
ring bearer

Reputation: 20783

req.getContextPath();

Should get you the context path you are looking for.

Upvotes: 0

Related Questions