I
I
IBM7012016-05-02 13:07:44
Java
IBM701, 2016-05-02 13:07:44

How to get Spring MVC Method URL?

There are controller methods that are annotated with my annotation. Roughly speaking, I have a list of methods and I need to get the URL of these methods. For example:

@RequestMapping("/test")
@Controller
public class TestController {

    @RequestMapping(method = RequestMethod.GET)
    public String methodOne(){}

    @RequestMapping(value = "/2", method = RequestMethod.GET)
    public String methodTwi(){}

}

I want to get:
for methodOne: /test
for methodTwo: /test/2

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2016-05-02
@zolt85

Scan the classpath, get all the classes annotated as @Controller, then walk through the found classes, collect methods annotated with @RequestMapping... that's the magic.

N
Noobkesan, 2016-05-04
@Noobkesan

@RequestMapping(method = RequestMethod.GET)
methodOne(HttpServletRequest request) {
System.out.println(request.getRequestURL().toString());
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question