ITeye博客

spring mvc 获取所有的controller和url映射关系

jayung · 2015-09-05 17:38 · 企业架构 · Markdown

有时候需要根据url反查controller,如果能获取所有的url,则不用跟据url去代码里搜了,方便开发人员、调试人员或交接人。

关键对象:RequestMappingHandlerMapping 

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
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.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Controller
public class MappingController {

	@Autowired
	private RequestMappingHandlerMapping requestMappingHandlerMapping;

	@RequestMapping(value = "/mappings")
	public String list(Model model) {
		List<HashMap<String, String>> urlList = new ArrayList<HashMap<String, String>>();

		Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
		for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
			HashMap<String, String> hashMap = new HashMap<String, String>();
			RequestMappingInfo info = m.getKey();
			HandlerMethod method = m.getValue();
			PatternsRequestCondition p = info.getPatternsCondition();
			for (String url : p.getPatterns()) {
				hashMap.put("url", url);
			}
			hashMap.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
			hashMap.put("method", method.getMethod().getName()); // 方法名
			RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
			String type = methodsCondition.toString();
			if (type != null && type.startsWith("[") && type.endsWith("]")) {
				type = type.substring(1, type.length() - 1);
				hashMap.put("type", type); // 方法名
			}
			urlList.add(hashMap);
		}
		model.addAttribute("list", urlList);
		return "/console/system/mappingList";
	}

}

 然后再在页面上遍历list即可

	<table class="tableList" >
	<tr>
			<th>类名</th>
			<th>方法名</th>
			<th>URL</th>
			<th>类型</th>
	<tr>
		<c:forEach items="${list}" var="mvc" varStatus="status">
		<tr id="${status.index}">
			<td>${mvc.className}</td>
			<td>${mvc.method}</td>
			<td>
				<c:choose>
				<c:when test="${!fn:contains(mvc.url,'}') and (mvc.type=='GET' or mvc.type=='')}">
					<a href="${ctx}${mvc.url}" target="_blank">${mvc.url}</a>
				</c:when>
				<c:otherwise>${mvc.url}</c:otherwise>
				</c:choose>
			</td>
			<td>${mvc.type}</td>
		</tr>
		</c:forEach>
	</table>

评论(4)

  1. #4 楼 · bewithme · 2016-11-29 11:37

    ontext.support.XmlWebApplicationContext:544) - Exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourcesController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping com.ternnetwork.baseframework.controller.security.ResourcesController.requestMappingHandlerMapping; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.A

  2. #3 楼 · jayung · 2015-09-14 14:41

    lcwen_13 writes:

    只是取了其中一部分映射

    没发现啊,那全部的映射要怎么取,请教了

  3. #2 楼 · lcwen_13 · 2015-09-05 22:15

    只是取了其中一部分映射

  4. #1 楼 · white_crucifix · 2015-09-05 19:29

    想法还是不错的。当然IDE也已经提供了。 IntelliJ idea和spring版eclipse,都会列出所有的路由,并关联到对应的controller方法上。

评论功能维护中,当前仅展示历史评论。