2012-03-19 59 views
15

我有一個沒有標準的Spring MVC項目。用XML進行響應。是否可以創建一個視圖(jsp頁面)顯示所有控制器,映射和接受的參數(不需要)。如何顯示視圖中的所有控制器和映射

基於答案,我有:

@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; 
} 

我沒有在春季3.1得到@RequestParam

回答

26

隨着RequestMappingHandlerMapping任何信息,您可以輕鬆地瀏覽端點。

控制器:

@Autowire 
private RequestMappingHandlerMapping requestMappingHandlerMapping; 

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

的觀點:

<%@ 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> 

你也可以做到這一點與Spring < 3.1,與DefaultAnnotationHandlerMapping代替RequestMappingHandlerMapping。但是你不會有相同的信息等級。

隨着DefaultAnnotationHandlerMapping你只會有終點的路徑,沒有關於他們的方法的信息,消耗,參數...

+0

它是好的,但是,我沒有得到的所有信息。

 \t @RequestMapping("/get") \t public @ResponseBody \t String getUsername( \t \t \t @RequestParam(value = "id", required = true) int id) { \t \t \t \t return "test"; \t }
mamruoc 2012-03-22 11:37:54

+0

getPatternsCondition正在工作,但所有其他人不起作用。 – mamruoc 2012-03-22 11:43:51

+0

我爲自己的用法編寫了這段代碼,效果很好。這不是因爲'$ {endPoint.methodsCondition}'不顯示任何不起作用的東西。這只是因爲你沒有任何endPoint的方法條件。此外,'RequestMappingHandlerMapping'信息僅基於'@ RequestMapping'註解內容。如果你想看到參數'id',你必須有一個參數條件,類似這樣:'@RequestParam(value =「/ get」,params = {「id」})' – tbruyelle 2012-03-22 15:07:46

相關問題