Following controller cna be used to list out all URLs mapped in Spring MVC application. It uses a Spring's RequestMappingHandlerMapping class which creats and instance from all @RequestMapping of the @Controller annotation.
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@Controller
public class EndpointDocController {
private final RequestMappingHandlerMapping handlerMapping;
@Autowired
public EndpointDocController(RequestMappingHandlerMapping handlerMapping) {
this.handlerMapping = handlerMapping;
}
@RequestMapping(value = "/endpointdoc.htm", method = RequestMethod.GET)
public String show(Model model) {
Map methodList = this.handlerMapping.getHandlerMethods();
for (RequestMappingInfo name : methodList.keySet()) {
String key = name.toString();
String value = methodList.get(name).toString();
System.out.println(key + " " + value);
}
model.addAttribute("handlerMethods", this.handlerMapping.getHandlerMethods());
return "/view/controllerList";
}
}
Above code can print the result in console itself as well as send the result to the view. One can easily write view to iterate on Map to display result in web page.
No comments:
Post a Comment