17

I have a none standard Spring MVC project. Responding with XMLs. Is it possible to create a view (jsp page) showing all controllers, mappings and parameters that are accepted (required and not).

Based on answer,I have:

@RequestMapping(value= "/endpoints", params="secure",  method = RequestMethod.GET)
public @ResponseBody
String getEndPointsInView() {
    String result = "";
    for (RequestMappingInfo element : requestMappingHandlerMapping.getHandlerMethods().keySet()) {

        result += "<p>" + element.getPatternsCondition() + "<br>";
        result += element.getMethodsCondition() + "<br>";
        result += element.getParamsCondition() + "<br>";
        result += element.getConsumesCondition() + "<br>";
    }
    return result;
}

I don't get any information from @RequestParam

mamruoc
  • 847
  • 3
  • 11
  • 21

1 Answers1

32

With RequestMappingHandlerMapping in Spring 3.1, you can easily browse the endpoints.

The controller :

@Autowire
private RequestMappingHandlerMapping requestMappingHandlerMapping;

@RequestMapping( value = "endPoints", method = RequestMethod.GET )
public String getEndPointsInView( Model model )
{
    model.addAttribute( "endPoints", requestMappingHandlerMapping.getHandlerMethods().keySet() );
    return "admin/endPoints";
}

The view :

<%@ page session="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<head><title>Endpoint list</title></head>
<body>
<table>
  <thead>
  <tr>
    <th>path</th>
    <th>methods</th>
    <th>consumes</th>
    <th>produces</th>
    <th>params</th>
    <th>headers</th>
    <th>custom</th>
  </tr>
  </thead>
  <tbody>
  <c:forEach items="${endPoints}" var="endPoint">
    <tr>
      <td>${endPoint.patternsCondition}</td>
      <td>${endPoint.methodsCondition}</td>
      <td>${endPoint.consumesCondition}</td>
      <td>${endPoint.producesCondition}</td>
      <td>${endPoint.paramsCondition}</td>
      <td>${endPoint.headersCondition}</td>
      <td>${empty endPoint.customCondition ? "none" : endPoint.customCondition}</td>
    </tr>
  </c:forEach>
  </tbody>
</table>
</body>
</html>

You can also do this with Spring < 3.1, with DefaultAnnotationHandlerMapping instead of RequestMappingHandlerMapping. But you won't have the same level of information.

With DefaultAnnotationHandlerMapping you will only have the endpoints path, without information about their methods, consumes, params...

tbruyelle
  • 12,895
  • 9
  • 60
  • 74
  • It is nice, however, I do not get all information.
     @RequestMapping("/get")
     public @ResponseBody
     String getUsername(
       @RequestParam(value = "id", required = true) int id) {
      
      return "test";
     }
    – mamruoc Mar 22 '12 at 11:37
  • getPatternsCondition is working, but all the others does not work. – mamruoc Mar 22 '12 at 11:43
  • I wrote this code for my own usage and it works well. This is not because `${endPoint.methodsCondition}` displays nothing it doesn't work. It's just because you don't have any method conditions for the endPoint. Moreover, `RequestMappingHandlerMapping` informations are based only on the `@RequestMapping` annotation content. If you want to see the parameter `id`, you have to had a parameter condition, something like that : `@RequestParam(value="/get", params = {"id"})` – tbruyelle Mar 22 '12 at 15:07
  • Oki, I see. Got it working with @RequestMapping and params=something. Do you know how to access the fields in @RequestMapping (required and optinal)? - Man, I suck at stackoverflow editing – mamruoc Mar 22 '12 at 16:07
  • Lol, click on help at the bottom of `Add Comment` and you will see how to pad your code snippet. Oups I made a mistake in my last comment I meant `@RequestMapping` not `@RequestParam` for the last thing. The fields in `@RequestMapping` are conditions and they are al displayed in the solution I gave you. – tbruyelle Mar 22 '12 at 18:00
  • :-) I'll the help then. I am not using your example 100%. I'll update the question for better code reading. – mamruoc Mar 22 '12 at 21:36
  • +1 nice answer and _exactly_ what I was looking for. FWIW, I ended up just using a simple `return new ModelAndView("_system", "routes", requestMappingHandlerMapping.getHandlerMethods().keySet());` as I was using the `ModelAndView` in another method – andyb Sep 12 '12 at 12:37
  • am using spring 3.0 . is there anyway to achieve the same in spring 3.0 ?? – Arun Feb 27 '13 at 12:06
  • Yes, see the bottom of my answer concerning spring version < 3.1 – tbruyelle Feb 27 '13 at 13:59