Home > database >  spring boot get all request mappings path, paramters, response type
spring boot get all request mappings path, paramters, response type

Time:10-11

I want to get all request mappings. Include my controller mapping, filter mapping and framework mapping. It look like spring boot spring boot actuator /actuator/mappings, but I do not know it how to get all mappings from source. And I need the request mapping with paramter type and response type.

I try to these method:

  1. RequestMappingHandlerMapping: It will get controller mappings.
  2. I find actuator hander is org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping.OperationHandler#handle, but I do not know how to use it.
  3. org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints: It will get empty list or base path with /actuator.

How do I do? Thanks

CodePudding user response:

The question had be resolved.

Failure methods

I try to these method to get servlet mappings, but failed.

  1. Inject these discover: ControllerEndpointDiscoverer, WebEndpointDiscoverer and ServletEndpointDiscoverer. And init the endpoint like this:
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());

The output:

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 [].

These paths are incomplete.

  1. Inject WebApplicationContext and get ServletRegistration map:
Map<String, ? extends ServletRegistration> servletRegistrations = webApplicationContext.getServletContext().getServletRegistrations();
servletRegistrations.forEach((key, value) -> log.info("key {}, value {}", key, value.getMappings()));

The ouput:

2022-10-10T23:15:17.384 08:00  INFO 49374 --- [nio-9001-exec-1] w.l...: key dispatcherServlet, value [/]

He seems to be servlet base path.

  1. Inject RequestMappingHandlerMapping and getHandlerMethods:
Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
handlerMethods.forEach((key, value) -> log.info("key {}, value {}", key.getName(), value.getMethod()));

The output


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()

It only includes controller paths. I need to all mapping paths.

  1. Inject ExposableWebEndpoint collection.
for (ExposableWebEndpoint endpoint : endpoints) {
    for (WebOperation operation : endpoint.getOperations()) {
        String path = operation.getRequestPredicate().getPath();
        log.info("Path {}", path);
    }
}

Nothing output. And the endpoints only include actuator path.

Success method

Read org.springframework.boot:spring-boot-starter-actuator source, I find the /actuator/mappings handler is org.springframework.boot.actuate.web.mappings.MappingsEndpoint#mappings. And the mappings come from descriptionProviders.

So, inject 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));
}

The output

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

The mappings include controller, filter and more.

And I can get method or other information. I think the method is best.

Links:

  • org.springframework.boot.actuate.web.mappings.servlet.DispatcherServletMappingDetails
  • org.springframework.boot.actuate.web.mappings.HandlerMethodDescription
  • Related