1
1
package com .ubaid .ms .config ;
2
2
3
+ import java .lang .reflect .Field ;
4
+ import java .util .List ;
5
+ import org .springframework .beans .BeansException ;
6
+ import org .springframework .beans .factory .config .BeanPostProcessor ;
3
7
import org .springframework .boot .SpringApplication ;
4
8
import org .springframework .boot .autoconfigure .SpringBootApplication ;
5
9
import org .springframework .cloud .config .server .EnableConfigServer ;
10
+ import org .springframework .context .annotation .Bean ;
11
+ import org .springframework .lang .NonNull ;
12
+ import org .springframework .util .ReflectionUtils ;
13
+ import org .springframework .web .servlet .mvc .method .RequestMappingInfoHandlerMapping ;
14
+ import springfox .documentation .spring .web .plugins .WebFluxRequestHandlerProvider ;
15
+ import springfox .documentation .spring .web .plugins .WebMvcRequestHandlerProvider ;
6
16
7
17
@ EnableConfigServer
8
18
@ SpringBootApplication
@@ -12,4 +22,41 @@ public static void main(String[] args) {
12
22
SpringApplication .run (ConfigServer .class , args );
13
23
}
14
24
25
+ @ Bean
26
+ public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor () {
27
+ return new BeanPostProcessor () {
28
+
29
+ @ Override
30
+ public Object postProcessAfterInitialization (@ NonNull Object bean , @ NonNull String beanName ) throws BeansException {
31
+ if (bean instanceof WebMvcRequestHandlerProvider
32
+ || bean instanceof WebFluxRequestHandlerProvider ) {
33
+ customizeSpringfoxHandlerMappings (getHandlerMappings (bean ));
34
+ }
35
+ return bean ;
36
+ }
37
+
38
+ private <T extends RequestMappingInfoHandlerMapping > void customizeSpringfoxHandlerMappings (
39
+ List <T > mappings ) {
40
+ List <T > copy = mappings .stream ()
41
+ .filter (mapping -> mapping .getPatternParser () == null )
42
+ .toList ();
43
+ mappings .clear ();
44
+ mappings .addAll (copy );
45
+ }
46
+
47
+ @ SuppressWarnings ("unchecked" )
48
+ private List <RequestMappingInfoHandlerMapping > getHandlerMappings (Object bean ) {
49
+ try {
50
+ Field field = ReflectionUtils .findField (bean .getClass (), "handlerMappings" );
51
+ assert field != null ;
52
+ field .setAccessible (true );
53
+ return (List <RequestMappingInfoHandlerMapping >) field .get (bean );
54
+ } catch (IllegalArgumentException | IllegalAccessException e ) {
55
+ throw new IllegalStateException (e );
56
+ }
57
+ }
58
+ };
59
+ }
60
+
61
+
15
62
}
0 commit comments