Created
December 24, 2021 11:38
-
-
Save leovegas/c6ba77ea437bf4c6b204229bf9a368b7 to your computer and use it in GitHub Desktop.
Springfox swagger2 3.0.0 BeanPostProcessor for Spring Boot 2.6.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Bean | |
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() { | |
return new BeanPostProcessor() { | |
@Override | |
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | |
if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) { | |
customizeSpringfoxHandlerMappings(getHandlerMappings(bean)); | |
} | |
return bean; | |
} | |
private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) { | |
List<T> copy = mappings.stream() | |
.filter(mapping -> mapping.getPatternParser() == null) | |
.collect(Collectors.toList()); | |
mappings.clear(); | |
mappings.addAll(copy); | |
} | |
@SuppressWarnings("unchecked") | |
private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) { | |
try { | |
Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings"); | |
field.setAccessible(true); | |
return (List<RequestMappingInfoHandlerMapping>) field.get(bean); | |
} catch (IllegalArgumentException | IllegalAccessException e) { | |
throw new IllegalStateException(e); | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment