Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

spring - How to show all controllers and mappings in a view

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...