|
17 | 17 | package org.springframework.samples.petclinic.util; |
18 | 18 |
|
19 | 19 |
|
| 20 | +import org.springframework.beans.BeansException; |
| 21 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
20 | 22 | import org.springframework.context.annotation.Bean; |
21 | 23 | import org.springframework.context.annotation.ComponentScan; |
22 | 24 | import org.springframework.context.annotation.Configuration; |
23 | 25 |
|
| 26 | +import org.springframework.util.ReflectionUtils; |
| 27 | +import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; |
24 | 28 | import springfox.documentation.builders.PathSelectors; |
25 | 29 | import springfox.documentation.builders.RequestHandlerSelectors; |
26 | 30 | import springfox.documentation.service.ApiInfo; |
27 | 31 | import springfox.documentation.service.Contact; |
28 | 32 | import springfox.documentation.spi.DocumentationType; |
29 | 33 | import springfox.documentation.spring.web.plugins.Docket; |
| 34 | +import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider; |
| 35 | +import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider; |
30 | 36 | import springfox.documentation.swagger2.annotations.EnableSwagger2; |
31 | 37 |
|
| 38 | +import java.lang.reflect.Field; |
32 | 39 | import java.util.Collections; |
| 40 | +import java.util.List; |
| 41 | +import java.util.stream.Collectors; |
33 | 42 |
|
34 | 43 | /** |
35 | 44 | * Java config for Springfox swagger documentation plugin |
@@ -67,5 +76,38 @@ private ApiInfo getApiInfo() { |
67 | 76 | "http://www.apache.org/licenses/LICENSE-2.0", Collections.emptyList()); |
68 | 77 | } |
69 | 78 |
|
| 79 | + /** |
| 80 | + * Springfox workaround required by Spring Boot 2.6 |
| 81 | + * See https://github.com/springfox/springfox/issues/346 |
| 82 | + */ |
| 83 | + @Bean |
| 84 | + public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() { |
| 85 | + return new BeanPostProcessor() { |
| 86 | + |
| 87 | + @Override |
| 88 | + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 89 | + if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) { |
| 90 | + customizeSpringfoxHandlerMappings(getHandlerMappings(bean)); |
| 91 | + } |
| 92 | + return bean; |
| 93 | + } |
| 94 | + |
| 95 | + private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) { |
| 96 | + mappings.removeIf(mapping -> mapping.getPatternParser() != null); |
| 97 | + } |
| 98 | + |
| 99 | + @SuppressWarnings("unchecked") |
| 100 | + private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) { |
| 101 | + try { |
| 102 | + Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings"); |
| 103 | + field.setAccessible(true); |
| 104 | + return (List<RequestMappingInfoHandlerMapping>) field.get(bean); |
| 105 | + } catch (IllegalArgumentException | IllegalAccessException e) { |
| 106 | + throw new IllegalStateException(e); |
| 107 | + } |
| 108 | + } |
| 109 | + }; |
| 110 | + } |
| 111 | + |
70 | 112 |
|
71 | 113 | } |
0 commit comments