1
1
package app ;
2
2
3
+ import org .springframework .beans .BeansException ;
4
+ import org .springframework .beans .factory .config .BeanPostProcessor ;
3
5
import org .springframework .context .annotation .Bean ;
4
6
import org .springframework .context .annotation .Configuration ;
7
+ import org .springframework .util .ReflectionUtils ;
8
+ import org .springframework .web .servlet .mvc .method .RequestMappingInfoHandlerMapping ;
5
9
import springfox .documentation .builders .PathSelectors ;
6
10
import springfox .documentation .builders .RequestHandlerSelectors ;
7
11
import springfox .documentation .spi .DocumentationType ;
8
12
import springfox .documentation .spring .web .plugins .Docket ;
13
+ import springfox .documentation .spring .web .plugins .WebFluxRequestHandlerProvider ;
14
+ import springfox .documentation .spring .web .plugins .WebMvcRequestHandlerProvider ;
15
+
16
+ import java .lang .reflect .Field ;
17
+ import java .util .List ;
18
+ import java .util .stream .Collectors ;
9
19
10
20
/**
11
21
* @author zhihao zhang
@@ -21,4 +31,41 @@ public Docket api() {
21
31
.paths (PathSelectors .any ())
22
32
.build ();
23
33
}
34
+
35
+ @ 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
+ }
58
+
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
+ };
70
+ }
24
71
}
0 commit comments