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

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

关键对象:RequestMappingHandlerMapping

Java代码  

  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.ui.Model;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.method.HandlerMethod;
  10. import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
  11. import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
  12. import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
  13. import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
  14. @Controller
  15. public class MappingController {
  16. @Autowired
  17. private RequestMappingHandlerMapping requestMappingHandlerMapping;
  18. @RequestMapping(value = "/mappings")
  19. public String list(Model model) {
  20. List<HashMap<String, String>> urlList = new ArrayList<HashMap<String, String>>();
  21. Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
  22. for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
  23. HashMap<String, String> hashMap = new HashMap<String, String>();
  24. RequestMappingInfo info = m.getKey();
  25. HandlerMethod method = m.getValue();
  26. PatternsRequestCondition p = info.getPatternsCondition();
  27. for (String url : p.getPatterns()) {
  28. hashMap.put("url", url);
  29. }
  30. hashMap.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
  31. hashMap.put("method", method.getMethod().getName()); // 方法名
  32. RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
  33. String type = methodsCondition.toString();
  34. if (type != null && type.startsWith("[") && type.endsWith("]")) {
  35. type = type.substring(1, type.length() - 1);
  36. hashMap.put("type", type); // 方法名
  37. }
  38. urlList.add(hashMap);
  39. }
  40. model.addAttribute("list", urlList);
  41. return "/console/system/mappingList";
  42. }
  43. }
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即可java框架源码下载】

Java代码  

  1. <table class="tableList" >
  2. <tr>
  3. <th>类名</th>
  4. <th>方法名</th>
  5. <th>URL</th>
  6. <th>类型</th>
  7. <tr>
  8. <c:forEach items="${list}" var="mvc" varStatus="status">
  9. <tr id="${status.index}">
  10. <td>${mvc.className}</td>
  11. <td>${mvc.method}</td>
  12. <td>
  13. <c:choose>
  14. <c:when test="${!fn:contains(mvc.url,‘}‘) and (mvc.type==‘GET‘ or mvc.type==‘‘)}">
  15. <a href="${ctx}${mvc.url}" target="_blank">${mvc.url}</a>
  16. </c:when>
  17. <c:otherwise>${mvc.url}</c:otherwise>
  18. </c:choose>
  19. </td>
  20. <td>${mvc.type}</td>
  21. </tr>
  22. </c:forEach>
  23. </table>
	<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>

2

1

时间: 2024-08-29 05:55:07

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

Spring MVC 从页面向Controller传递参数的方式

Spring MVC从页面向Controller传值的方式 从页面向Controller 传值的方式有很多,本文整理出常用的从页面向Controller传值的几种方式: 1.通过HttpServletRequest 向后台传递数据      页面程序如下: <h2>利用HttpServletRequest传数据</h2> <form action="loginToJson"> 用户名:<input name="name" /

spring mvc获取路径参数的几种方式 - 浅夏的个人空间 - 开源中国社区

body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI",Tahoma,Helvetica,Sans-Serif,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif; font-size: 10.5pt; line-height: 1.5;

spring mvc 获取所有注册的url

@RequestMapping("getAllUrl") @ResponseBody public Set<String> getAllUrl(HttpServletRequest request) { Set<String> result = new HashSet<String>(); WebApplicationContext wc = (WebApplicationContext) request.getAttribute(Dispatche

Spring MVC 获取前端参数的注解

在与前端交互的开发过程中,出现过几次无法取到参数的情况,费了些时间去排查问题,下面就简单总结一下. 注解详解 我们所要获取的前端传递参数大概可以分为以下四类: requet uri 部分的注解:@PathVariable request header部分的注解:@RequestHeader, @CookieValue request body部分的注解:@RequestParam,  @RequestBody attribute 类型是注解: @SessionAttributes, @Model

Spring MVC学习笔记——给Controller传值

1.修改HelloController.java //RequestMapping表示用哪一个url来对应 @RequestMapping({"/hello","/"}) public String hello(@RequestParam("username") String username){ System.out.println("hello"); System.out.println(username); return

spring mvc获取绝对路径的几种方法

1.首先如果是在一个controller方法中,则很简单,直接用下面语句. 1 @RequestMapping("categoryHome") 2 public ModelAndView categoryHome(ParamModel pm,HttpServletRequest req) { 3 String path=req.getServletContext().getContextPath(); 4 System.out.println(path); 5 String realP

spring MVC 获取全局Session

1 /** 2 * 获取全局Session 3 * @return 4 */ 5 @SuppressWarnings("unchecked") 6 public static String findAdminFromSession(){ 7 try{ 8 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); 9

spring mvc获取header

两种方法: 1.在方法参数中加入@RequestHeader 2.在类级别注入HttpServletRequest 建议使用第二种方法,这样可避免每个方法都加入HttpHeaders参数 @Controller @RequestMapping("/hello") public class HelloController { @Autowired private HttpServletRequest request; @RequestMapping(value="/printn

spring MVC 获取servletContext,实现文件下载功能

以下是获取servletContext: import javax.servlet.ServletContext; import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; /** * ServletContext辅助类.提供springmvc获取servletContext对象及项目真实路径的静态方法 * @author