集成swagger 报错

不添加swagger时运行正常,添加后服务启动报错如图,

Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled.
2021-11-25 21:00:51.051 ERROR 2870 — [ main] o.s.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) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.13.jar:5.3.13]
at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_201]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) [spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) [spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) [spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) [spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290) [spring-boot-2.6.0.jar:2.6.0]
at com.hogwartsmini.demo.DemoApplication.main(DemoApplication.java:10) [classes/:na]
Caused by: java.lang.NullPointerException: null
at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:112) ~[springfox-spi-2.9.2.jar:null]
at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:109) ~[springfox-spi-2.9.2.jar:null]
at com.google.common.collect.ComparatorOrdering.compare(ComparatorOrdering.java:40) ~[guava-31.0.1-jre.jar:na]
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) ~[na:1.8.0_201]
at java.util.TimSort.sort(TimSort.java:220) ~[na:1.8.0_201]
at java.util.Arrays.sort(Arrays.java:1438) ~[na:1.8.0_201]
at com.google.common.collect.Ordering.sortedCopy(Ordering.java:854) ~[guava-31.0.1-jre.jar:na]
at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:57) ~[springfox-spring-web-2.9.2.jar:null]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:138) ~[springfox-spring-web-2.9.2.jar:null]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:135) ~[springfox-spring-web-2.9.2.jar:null]
at com.google.common.collect.Iterators$6.transform(Iterators.java:826) ~[guava-31.0.1-jre.jar:na]
at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:52) ~[guava-31.0.1-jre.jar:na]
at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:52) ~[guava-31.0.1-jre.jar:na]
at com.google.common.collect.Iterators$ConcatenatedIterator.hasNext(Iterators.java:1404) ~[guava-31.0.1-jre.jar:na]
at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:275) ~[guava-31.0.1-jre.jar:na]
at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:239) ~[guava-31.0.1-jre.jar:na]
at com.google.common.collect.FluentIterable.toList(FluentIterable.java:631) ~[guava-31.0.1-jre.jar:na]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.defaultContextBuilder(DocumentationPluginsBootstrapper.java:111) ~[springfox-spring-web-2.9.2.jar:null]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.buildContext(DocumentationPluginsBootstrapper.java:96) ~[springfox-spring-web-2.9.2.jar:null]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167) ~[springfox-spring-web-2.9.2.jar:null]
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-5.3.13.jar:5.3.13]
… 14 common frames omitted

Process finished with exit code 1

附上类:
@Configuration
@EnableSwagger2
public class SwaggerConfig {

@Bean
public Docket docket() {
    ParameterBuilder builder = new ParameterBuilder();
    builder.parameterType("header").name("token")
            .description("token值")
            .required(true)
            .modelRef(new ModelRef("string")); // 在swagger里显示header

    return new Docket(DocumentationType.SWAGGER_2)
            .groupName("aitest_interface")
            .apiInfo(apiInfo())
            .globalOperationParameters(Lists.newArrayList(builder.build()))
            .select().paths(PathSelectors.any()).build();
}

private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
            .title("aitest-mini系统")
            .description("aitest-mini接口文档")
            .contact(new Contact("zhou", "", "15675154076@163.com"))
            .version("1.0")
            .build();
}

}

解决:springboot降低版本

1 Like

需要降低到什么版本呢?我也遇到此问题了。困扰了几天

关闭