11package com .zzhpro .movie ;
22
3- import org .springframework .beans .BeansException ;
4- import org .springframework .beans .factory .config .BeanPostProcessor ;
3+ import org .springframework .boot .actuate .autoconfigure .endpoint .web .CorsEndpointProperties ;
4+ import org .springframework .boot .actuate .autoconfigure .endpoint .web .WebEndpointProperties ;
5+ import org .springframework .boot .actuate .autoconfigure .web .server .ManagementPortType ;
6+ import org .springframework .boot .actuate .endpoint .ExposableEndpoint ;
7+ import org .springframework .boot .actuate .endpoint .web .EndpointLinksResolver ;
8+ import org .springframework .boot .actuate .endpoint .web .EndpointMapping ;
9+ import org .springframework .boot .actuate .endpoint .web .EndpointMediaTypes ;
10+ import org .springframework .boot .actuate .endpoint .web .ExposableWebEndpoint ;
11+ import org .springframework .boot .actuate .endpoint .web .WebEndpointsSupplier ;
12+ import org .springframework .boot .actuate .endpoint .web .annotation .ControllerEndpointsSupplier ;
13+ import org .springframework .boot .actuate .endpoint .web .annotation .ServletEndpointsSupplier ;
14+ import org .springframework .boot .actuate .endpoint .web .servlet .WebMvcEndpointHandlerMapping ;
515import org .springframework .context .annotation .Bean ;
616import org .springframework .context .annotation .Configuration ;
7- import org .springframework .util . ReflectionUtils ;
8- import org .springframework .web . servlet . mvc . method . RequestMappingInfoHandlerMapping ;
17+ import org .springframework .core . env . Environment ;
18+ import org .springframework .util . StringUtils ;
919import springfox .documentation .builders .PathSelectors ;
1020import springfox .documentation .builders .RequestHandlerSelectors ;
1121import springfox .documentation .spi .DocumentationType ;
1222import springfox .documentation .spring .web .plugins .Docket ;
13- import springfox .documentation .spring .web .plugins .WebFluxRequestHandlerProvider ;
14- import springfox .documentation .spring .web .plugins .WebMvcRequestHandlerProvider ;
1523
16- import java .lang .reflect .Field ;
24+ import java .util .ArrayList ;
25+ import java .util .Collection ;
1726import java .util .List ;
18- import java .util .stream .Collectors ;
1927
2028/**
2129 * @author zhihao zhang
@@ -33,39 +41,27 @@ public Docket api() {
3341 }
3442
3543 @ Bean
36- public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor () {
37- return new BeanPostProcessor () {
38-
39- @ Override
40- public Object postProcessAfterInitialization (Object bean , String beanName )
41- throws BeansException {
42- if (bean instanceof WebMvcRequestHandlerProvider
43- || bean instanceof WebFluxRequestHandlerProvider ) {
44- customizeSpringfoxHandlerMappings (getHandlerMappings (bean ));
45- }
46- return bean ;
47- }
48-
49- private <T extends RequestMappingInfoHandlerMapping > void customizeSpringfoxHandlerMappings (
50- List <T > mappings ) {
51- List <T > copy =
52- mappings .stream ()
53- .filter (mapping -> mapping .getPatternParser () == null )
54- .collect (Collectors .toList ());
55- mappings .clear ();
56- mappings .addAll (copy );
57- }
44+ public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping (WebEndpointsSupplier webEndpointsSupplier ,
45+ ServletEndpointsSupplier servletEndpointsSupplier , ControllerEndpointsSupplier controllerEndpointsSupplier ,
46+ EndpointMediaTypes endpointMediaTypes , CorsEndpointProperties corsProperties ,
47+ WebEndpointProperties webEndpointProperties , Environment environment ) {
48+ List <ExposableEndpoint <?>> allEndpoints = new ArrayList <>();
49+ Collection <ExposableWebEndpoint > webEndpoints = webEndpointsSupplier .getEndpoints ();
50+ allEndpoints .addAll (webEndpoints );
51+ allEndpoints .addAll (servletEndpointsSupplier .getEndpoints ());
52+ allEndpoints .addAll (controllerEndpointsSupplier .getEndpoints ());
53+ String basePath = webEndpointProperties .getBasePath ();
54+ EndpointMapping endpointMapping = new EndpointMapping (basePath );
55+ boolean shouldRegisterLinksMapping = this .shouldRegisterLinksMapping (webEndpointProperties , environment ,
56+ basePath );
57+ return new WebMvcEndpointHandlerMapping (endpointMapping , webEndpoints , endpointMediaTypes ,
58+ corsProperties .toCorsConfiguration (), new EndpointLinksResolver (allEndpoints , basePath ),
59+ shouldRegisterLinksMapping , null );
60+ }
5861
59- @ SuppressWarnings ("unchecked" )
60- private List <RequestMappingInfoHandlerMapping > getHandlerMappings (Object bean ) {
61- try {
62- Field field = ReflectionUtils .findField (bean .getClass (), "handlerMappings" );
63- field .setAccessible (true );
64- return (List <RequestMappingInfoHandlerMapping >) field .get (bean );
65- } catch (IllegalArgumentException | IllegalAccessException e ) {
66- throw new IllegalStateException (e );
67- }
68- }
69- };
62+ private boolean shouldRegisterLinksMapping (WebEndpointProperties webEndpointProperties , Environment environment ,
63+ String basePath ) {
64+ return webEndpointProperties .getDiscovery ().isEnabled () && (StringUtils .hasText (basePath )
65+ || ManagementPortType .get (environment ).equals (ManagementPortType .DIFFERENT ));
7066 }
7167}
0 commit comments