1

I have controller's methods annotated by my custom annotation. When I select them I receive list of methods and I want to get URLs for each of those 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(){}

}

And I want to recieve:

for methodOne - /test

for methodTwo - /test/2

Minh Kieu
  • 475
  • 3
  • 9
Nikita
  • 1,465
  • 3
  • 15
  • 17
  • What is your question? – Minh Kieu May 02 '16 at 10:53
  • How can I get those urls from spring mapping? – Nikita May 02 '16 at 10:55
  • maybe the answer provided here does what you want? https://stackoverflow.com/questions/10898056/how-to-find-all-controllers-in-spring-mvc/10899118#10899118 – Lutz Müller May 02 '16 at 11:21
  • Not exactly. I don't wanna find all controllers. I just want to get method url by method if exists. There is one method in RequestMappingHandlerMapping which called getMappingForMethod, but it's protected. – Nikita May 02 '16 at 11:33

1 Answers1

1

Something like this maybe. First write a controller with 2 methods mapping 3 URL's:

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

    @RequestMapping(method = RequestMethod.GET)
    public String firstMethod(Model model) {
        model.addAttribute("name", "Olivier");
        return "firstPage";
    }

    @RequestMapping(method = RequestMethod.GET, value = {"/demo", "/demo1"})
    public String secondMethod(Model model) {
        model.addAttribute("name", "Olivier");
        return "secondPage";
    }
}

Then in your @Configuration class, create the RequestMappingHandlerMapping bean:

@Configuration
public class DemoSpringApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoSpringApplication.class, args);
    }

    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
        return mapping;
    }
}

Create a special controller that inverts the Map:

@Controller
@RequestMapping("/requests")
public class RequestMappingController {
    @Autowired
    private RequestMappingHandlerMapping handler;

    @RequestMapping(method = RequestMethod.GET)
    public String showDoc(Model model) {
        model.addAttribute("methods", handler.getHandlerMethods());
        Map<RequestMappingInfo, HandlerMethod> map = handler.getHandlerMethods();

        Set<RequestMappingInfo> mappings = map.keySet();
        Map<String, String> reversedMap = new HashMap<String, String>();
        for(RequestMappingInfo info : mappings) {
            HandlerMethod method = map.get(info);
            reversedMap.put(method.toString(), info.getPatternsCondition().toString());
        }
        model.addAttribute("methods", reversedMap);

        return "showDoc";
    }
}

In the showDoc page, iterate through the newly constructed map:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Documentation</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Documentation of Spring MVC URL's</h1>
    <p th:each="methods: ${methods}">
        <p><span th:text="'JAVA method:' + ${methods.key} + ', URL:' + ${methods.value}"></span></p>
    </p>
</body>
</html>

This will give the following view (ordered by method and provides the mapped URL's):

Documentation of Spring MVC URL's

JAVA method:public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse), URL:[/error]

JAVA method:public java.lang.String com.demo.DemoController.firstMethod(org.springframework.ui.Model), URL:[/test]

JAVA method:public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest), URL:[/error]

JAVA method:public java.lang.String com.demo.DemoController.secondMethod(org.springframework.ui.Model), URL:[/test/demo || /test/demo1]

JAVA method:public java.lang.String com.demo.RequestMappingController.showDoc(org.springframework.ui.Mode