我的控制器方法由我的自定义注释注释。当我选择它们时,我会收到方法列表,我想获取每种方法的URL。
例如:
@RequestMapping("/test")
@Controller
public class TestController {
@RequestMapping(method = RequestMethod.GET)
public String methodOne(){}
@RequestMapping(value = "/2", method = RequestMethod.GET)
public String methodTwi(){}
}
我想收到:
for methodOne - / test
for methodTwo - / test / 2
答案 0 :(得分:0)
这样的事可能。 首先使用映射3个URL的2个方法编写一个控制器:
@Controller
@RequestMapping("/test")
public class DemoController {
@RequestMapping(method = RequestMethod.GET)
public String firstMethod(Model model) {
model.addAttribute("name", "Olivier");
return "firstPage";
}
@RequestMapping(method = RequestMethod.GET, value = {"/demo", "/demo1"})
public String secondMethod(Model model) {
model.addAttribute("name", "Olivier");
return "secondPage";
}
}
然后在你的@Configuration类中创建RequestMappingHandlerMapping bean:
@Configuration
public class DemoSpringApplication {
public static void main(String[] args) {
SpringApplication.run(DemoSpringApplication.class, args);
}
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
return mapping;
}
}
创建一个反转Map的特殊控制器:
@Controller
@RequestMapping("/requests")
public class RequestMappingController {
@Autowired
private RequestMappingHandlerMapping handler;
@RequestMapping(method = RequestMethod.GET)
public String showDoc(Model model) {
model.addAttribute("methods", handler.getHandlerMethods());
Map<RequestMappingInfo, HandlerMethod> map = handler.getHandlerMethods();
Set<RequestMappingInfo> mappings = map.keySet();
Map<String, String> reversedMap = new HashMap<String, String>();
for(RequestMappingInfo info : mappings) {
HandlerMethod method = map.get(info);
reversedMap.put(method.toString(), info.getPatternsCondition().toString());
}
model.addAttribute("methods", reversedMap);
return "showDoc";
}
}
在showDoc页面中,遍历新构建的地图:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Documentation</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Documentation of Spring MVC URL's</h1>
<p th:each="methods: ${methods}">
<p><span th:text="'JAVA method:' + ${methods.key} + ', URL:' + ${methods.value}"></span></p>
</p>
</body>
</html>
这将提供以下视图(按方法排序并提供映射的URL):
Spring MVC URL的文档
JAVA方法:public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse),URL:[ /错误]
JAVA方法:public java.lang.String com.demo.DemoController.firstMethod(org.springframework.ui.Model),URL:[/ test]
JAVA方法:public org.springframework.http.ResponseEntity&gt; org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest),URL:[/ error]
JAVA方法:public java.lang.String com.demo.DemoController.secondMethod(org.springframework.ui.Model),URL:[/ test / demo || /测试/ demo1的]
JAVA方法:public java.lang.String com.demo.RequestMappingController.showDoc(org.springframework.ui.Mode