我想要获得所有的请求映射。包括我的控制器映射、过滤器映射和框架映射。它看起来像spring-bootspring boot actuator /actuator/mappings
,但我不知道如何从源代码获取所有映射。我需要具有参数类型和响应类型的请求映射。
我尝试这些方法:
RequestMappingHandlerMapping
:它将获得控制器映射- 我发现执行器手柄是
org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping.OperationHandler#handle
,但我不知道如何使用它 org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints
:它将获得空列表或带有/actuator
的基本路径
我该怎么办?感谢
问题已经解决。
故障方法
我尝试使用这些方法来获取servlet映射,但失败了。
- 注入这些发现:
ControllerEndpointDiscoverer
、WebEndpointDiscoverer
和ServletEndpointDiscoverer
。并像这样初始化endpoint
:
PathMappedEndpoints pathMappedEndpoints = new PathMappedEndpoints(null, servletEndpointDiscoverer);
PathMappedEndpoints webEndpoints = new PathMappedEndpoints(null, webEndpointDiscoverer);
PathMappedEndpoints controllerEndpoints = new PathMappedEndpoints(null, controllerEndpointDiscoverer);
log.info("All path endpoints {}.", pathMappedEndpoints.getAllPaths());
log.info("All web endpoints {}.", webEndpoints.getAllPaths());
log.info("All controller endpoints {}.", controllerEndpoints.getAllPaths());
输出:
2022-10-10T23:15:17.381+08:00 INFO 49374 --- [nio-9001-exec-1] w.l...: All path endpoints [].
2022-10-10T23:15:17.382+08:00 INFO 49374 --- [nio-9001-exec-1] w.l...: All web endpoints [/beans, /caches, /health, /info, /conditions, /configprops, /env, /loggers, /heapdump, /threaddump, /metrics, /scheduledtasks, /mappings].
2022-10-10T23:15:17.382+08:00 INFO 49374 --- [nio-9001-exec-1] w.l...: All controller endpoints [].
这些路径不完整。
- 注入
WebApplicationContext
并获得ServletRegistration
映射:
Map<String, ? extends ServletRegistration> servletRegistrations = webApplicationContext.getServletContext().getServletRegistrations();
servletRegistrations.forEach((key, value) -> log.info("key {}, value {}", key, value.getMappings()));
输出:
2022-10-10T23:15:17.384+08:00 INFO 49374 --- [nio-9001-exec-1] w.l...: key dispatcherServlet, value [/]
他似乎是servlet的基本路径。
- 注入
RequestMappingHandlerMapping
和getHandlerMethods
:
Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
handlerMethods.forEach((key, value) -> log.info("key {}, value {}", key.getName(), value.getMethod()));
输出
2022-10-10T23:15:17.385+08:00 INFO 49374 --- [nio-9001-exec-1] w.l...: key null, value public org.springframework.http.HttpEntity wiki.lever.controller.AuthorizationController.tokenInfo(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken)
2022-10-10T23:15:17.385+08:00 INFO 49374 --- [nio-9001-exec-1] w.l...: key null, value public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(jakarta.servlet.http.HttpServletRequest,jakarta.servlet.http.HttpServletResponse)
2022-10-10T23:15:17.385+08:00 INFO 49374 --- [nio-9001-exec-1] w.l...: key null, value public org.springframework.http.ResponseEntity org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(jakarta.servlet.http.HttpServletRequest)
2022-10-10T23:15:17.386+08:00 INFO 49374 --- [nio-9001-exec-1] w.l...: key null, value public org.springframework.http.HttpEntity wiki.lever.controller.HelloController.hello(java.lang.String,int)
2022-10-10T23:15:17.386+08:00 INFO 49374 --- [nio-9001-exec-1] w.l...: key null, value public org.springframework.http.HttpEntity wiki.lever.controller.HelloController.word()
2022-10-10T23:15:17.386+08:00 INFO 49374 --- [nio-9001-exec-1] w.l...: key null, value public org.springframework.http.HttpEntity wiki.lever.controller.InformationController.mapping()
它只包括控制器路径。我需要映射所有路径。
- 注入
ExposableWebEndpoint
集合
for (ExposableWebEndpoint endpoint : endpoints) {
for (WebOperation operation : endpoint.getOperations()) {
String path = operation.getRequestPredicate().getPath();
log.info("Path {}", path);
}
}
无输出。端点仅包括执行器路径。
成功方法
读取org.springframework.boot:spring-boot-starter-actuator
源,我发现/actuator/mappings
处理程序是org.springframework.boot.actuate.web.mappings.MappingsEndpoint#mappings
。映射来自CCD_ 18。
因此,注入Collection<MappingDescriptionProvider>
:
DispatcherServletsMappingDescriptionProvider servletMappingProvider = descriptionProviders.stream()
.filter(DispatcherServletsMappingDescriptionProvider.class::isInstance)
.map(DispatcherServletsMappingDescriptionProvider.class::cast)
.findFirst().orElseThrow(() -> new SystemException("Can not find dispatcher servlet mapping provider."));
Map<String, List<DispatcherServletMappingDescription>> mappingDesciptionMaps = servletMappingProvider.describeMappings(webApplicationContext);
for (List<DispatcherServletMappingDescription> descriptionList : mappingDesciptionMaps.values()) {
descriptionList.stream()
.map(DispatcherServletMappingDescription::getDetails)
.filter(Objects::nonNull)
.map(details -> details.getRequestMappingConditions().getPatterns())
.flatMap(Collection::stream)
.forEach(pattern -> log.info("mapping pattern {}", pattern));
}
输出
2022-10-10T23:54:42.613+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/mappings
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/loggers
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/env
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/metrics
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/metrics/{requiredMetricName}
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/caches
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/threaddump
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/threaddump
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/caches/{cache}
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/configprops
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/health
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/info
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/configprops/{prefix}
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/loggers/{name}
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/caches/{cache}
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/caches
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/loggers/{name}
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/health/**
2022-10-10T23:54:42.614+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/beans
2022-10-10T23:54:42.615+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/env/{toMatch}
2022-10-10T23:54:42.615+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator
2022-10-10T23:54:42.615+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/conditions
2022-10-10T23:54:42.615+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/scheduledtasks
2022-10-10T23:54:42.615+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /actuator/heapdump
2022-10-10T23:54:42.615+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /information/mapping
2022-10-10T23:54:42.615+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /authorization/tokenInfo
2022-10-10T23:54:42.615+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /error
2022-10-10T23:54:42.615+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /error
2022-10-10T23:54:42.615+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /word
2022-10-10T23:54:42.615+08:00 INFO 51806 --- [nio-9001-exec-2] w.l...: mapping pattern /hello
映射包括控制器、过滤器等。
我可以得到方法或其他信息。我认为这个方法是最好的。
链接:
org.springframework.boot.actuate.web.mappings.servlet.DispatcherServletMappingDetails
org.springframework.boot.actuate.web.mappings.HandlerMethodDescription