본문으로 바로가기

Spring에서 RequestMapping 전체 목록 화면에 출력 하기

category JAVA/Spring 2018. 3. 7. 11:01
반응형

Spring에서 RequestMapping 전체 목록 화면에 출력 하기



오늘 소개해 드릴 내용은 Spring에서 작성된 RequestMapping 전체 목록을 화면에 출력하는 내용입니다.

스프링으로 개발하다보면 전체 RequestMapping 리스트가 필요하게 되는데 다음과 같이 작성 하시면 됩니다.


아래 코드는 /endPoints URL 호출을 하게 되면 화면에 RequestMapping 리스트를 출력 할 수 있도록 데이터를 생성하는 과정입니다.


import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Description;

import org.springframework.core.MethodParameter;

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;


import com.xcurenet.annotations.AuditMenu;

import com.xcurenet.annotations.AuditOperation;

import com.xcurenet.common.util.Common;


@Controller

public class EndpointDocController {

@Autowired

private RequestMappingHandlerMapping requestMappingHandlerMapping;


@RequestMapping(value = "/endPoints")

public String getEndPointsInView(Model model) {

List<Map<String, String>> pages = new ArrayList<Map<String, String>>();

List<Map<String, String>> result = new ArrayList<Map<String, String>>();

Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();

for (Entry<RequestMappingInfo, HandlerMethod> elem : map.entrySet()) {

RequestMappingInfo key = elem.getKey();

HandlerMethod method = elem.getValue();


Map<String, String> item = new HashMap<String, String>();

item.put("path", Common.nvl(key.getPatternsCondition().getPatterns().toArray()[0]));

item.put("cls", method.getMethod().getDeclaringClass().getSimpleName());

item.put("method", method.getMethod().getName());


AuditMenu auditMenu = method.getMethod().getDeclaringClass().getAnnotation(AuditMenu.class);

if (auditMenu != null) {

item.put("menuId", auditMenu.value().getMenuId());

}


AuditOperation operation = method.getMethodAnnotation(AuditOperation.class);

if (operation != null) {

item.put("operation", operation.value().getOperation());

}


Description desc = method.getMethodAnnotation(Description.class);

if (desc != null) {

item.put("desc", desc.value());

}



StringBuffer sb = new StringBuffer();

for (MethodParameter param : method.getMethodParameters()) {

sb.append(param.getParameterType().getSimpleName()).append(", ");

}

if (sb.toString().length() > 0) {

item.put("param", sb.toString().substring(0, sb.toString().length() - 2));

} else {

item.put("param", "");

}

item.put("rtn", Common.nvl(method.getMethod().getReturnType().getSimpleName()));

if (Common.isEquals(item.get("rtn"), "XcnResponseVO")) {

result.add(item);

} else {

pages.add(item);

}

}

model.addAttribute("endPoints", result);

model.addAttribute("pages", pages);

return "/endPoints";

}

}






아래 소스코드는 간단하게 html 테이블로 RequestMapping 데이터를 출력해 보았습니다.


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false"%>

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

<html>

<head>

<title>Endpoint list</title>

<style type="text/css">


.tbl_list {

border-top: 2px solid #73abd1;

}


.tbl_list table {

table-layout: fixed;

word-break: break-all;

border-collapse: collapse;

border-spacing: 0px;

}


.tbl_list table thead td,th {

background-color: #e8f3f9;

border-bottom: 1px #d7d7d7 solid;

border-right: 1px #d7d7d7 solid;

height: 30px;

color: #2a2a2a;

font-family: dotum, Verdana, arial, sans-serif;

text-align: center;

font-size: 12px;

font-weight: bold;

border-collapse: collapse;

border-spacing: 0px;

}


.tbl_list table thead td:last-child {

border-right: 0px;

}


.tbl_list table tbody td {

padding-left: 10px;

padding-right: 10px;

border-bottom: 1px #d7d7d7 solid;

border-right: 1px #d7d7d7 solid;

height: 30px;

color: #2a2a2a;

font-family: dotum, Verdana, arial, sans-serif;

font-size: 12px;

font-weight: none;

border-collapse: collapse;

border-spacing: 0px;

}


.tbl_list table tbody td:last-child {

border-right: 0px;

}

</style>

</head>

<body>

<h1>Request Pages Table</h1>

<div class="tbl_list">

<table>

<thead>

<tr>

<th>Desc</th>

<th>Path</th>

<th>Class</th>

<th>Method</th>

<th>Param</th>

<th>Return</th>

</tr>

</thead>

<tbody>

<c:forEach items="${pages}" var="endPoint">

<tr>

<td>${endPoint.desc}</td>

<td>${endPoint.path}</td>

<td>${endPoint.cls}</td>

<td>${endPoint.method}</td>

<td>${endPoint.param}</td>

<td>${endPoint.rtn}</td>

</tr>

</c:forEach>

</tbody>

</table>

</div>


<h1>Request Mapping Table</h1>

<div class="tbl_list">

<table>

<thead>

<tr>

<th>Desc</th>

<th>Operation</th>

<th>Path</th>

<th>Class</th>

<th>Method</th>

<th>Param</th>

<th>Return</th>

</tr>

</thead>

<tbody>

<c:forEach items="${endPoints}" var="endPoint">

<tr>

<td>${endPoint.desc}</td>

<td>${endPoint.operation}</td>

<td>${endPoint.path}</td>

<td>${endPoint.cls}</td>

<td>${endPoint.method}</td>

<td>${endPoint.param}</td>

<td>${endPoint.rtn}</td>

</tr>

</c:forEach>

</tbody>

</table>

</div>

</body>

</html>







화면을 요청하게 되면 다음과 같은 내용이 출력되는 것을 확인 할 수 있습니다.




반응형