'NullPointerException when upgrading Spring Boot to version 2.6.2

I upgraded by Spring Boot version from 2.5.4 to 2.6.2.

Now my app fails at startup because of this error - see below.

Any ideas how to fix it?

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. ||| 
2022-01-05 02:48:10.673 [main] ERROR org.springframework.boot.SpringApplication >>> Application run failed ||| org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181)
    at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)
    at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)
    at java.lang.Iterable.forEach(Iterable.java:75)
    at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155)
    at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:302)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290)
    at com.cmp.tpe.rcf.MicroserviceApplication.main(MicroserviceApplication.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
Caused by: java.lang.NullPointerException: null
    at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:112)
    at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:109)
    at com.google.common.collect.ComparatorOrdering.compare(ComparatorOrdering.java:37)
    at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
    at java.util.TimSort.sort(TimSort.java:220)
    at java.util.Arrays.sort(Arrays.java:1438)
    at com.google.common.collect.Ordering.sortedCopy(Ordering.java:855)
    at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:57)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:138)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:135)
    at com.google.common.collect.Iterators$7.transform(Iterators.java:750)
    at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47)
    at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47)
    at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:52)
    at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:50)
    at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:249)
    at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:209)
    at com.google.common.collect.FluentIterable.toList(FluentIterable.java:614)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.defaultContextBuilder(DocumentationPluginsBootstrapper.java:111)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.buildContext(DocumentationPluginsBootstrapper.java:96)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167)
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)
    ... 22 common frames omitted


Solution 1:[1]

Use Spring Migrator to know what all dependencies needs to be updated

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-properties-migrator</artifactId>
            <scope>compile</scope>
        </dependency>

Check this for more info - https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.6-Release-Notes

And also for Spring Fox - https://github.com/springfox/springfox/issues/3462

Solution 2:[2]

I know this does not solve your problem directly, but consider moving to springdoc. Springfox is so buggy at this point that is a pain to use. I've moved to springdoc 2 years ago because of its Spring WebFlux support and I am very happy about it. Additionally, it also supports Kotlin Coroutines, which I am not sure Springfox does.

If you decide to migrate, springdoc even has a migration guide.

Solution 3:[3]

The workaround mentioned in the below issue comment seems to be working for people with spring actuator as well.

https://github.com/springfox/springfox/issues/3462#issuecomment-1010721223

Solution 4:[4]

Springfox broke my JPA tests, and the actuator for Prometheus. It took several attempts to fix and was very annoying.

Necessary steps i had to do with Springboot version 2.6.3, and springdoc 1.6.8: (Also consult the migration guide mentioned by João Dias)

Using Gradle, throw out Springfox and add new dependency:

implementation 'org.springdoc:springdoc-openapi-ui:1.6.8'

Add some configuration, most simple version:

@Bean
public OpenAPI springAPI() {
    return new OpenAPI();
}

I still got a 404 error after that. So i added the swagger-ui resource location (a rather dirty hack):

registry.addResourceHandler("/swagger-ui/**")
 .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/");

After that i was able to call swagger-ui, the path will depend on your application context. In my case it is /api, so my URL is

http://127.0.0.1:8080/api/swagger-ui/index.html/

This then shows the stupid Swagger Petstore and i was not able to get rid of it for now. However, you can now enter the URL for your API (remember it will depend on your application context) enter image description here

And finally get your API displayed.

As a bonus i also ran into this bug, because a custom HTTPMessageConverter is what you need in case you want to use Prometheus. (Still working on getting the actuator back)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mansoor
Solution 2 João Dias
Solution 3 icyfire
Solution 4