Skip to content

Instantly share code, notes, and snippets.

@leovegas
Created December 24, 2021 11:38
Show Gist options
  • Save leovegas/c6ba77ea437bf4c6b204229bf9a368b7 to your computer and use it in GitHub Desktop.
Save leovegas/c6ba77ea437bf4c6b204229bf9a368b7 to your computer and use it in GitHub Desktop.
Springfox swagger2 3.0.0 BeanPostProcessor for Spring Boot 2.6.1
@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