怀夢 2021-05-20 10:41 采纳率: 50%
浏览 1556
已采纳

idea用spring boot框架引用Swagger报错这个...而且一直不能访问swagger

ERROR  Unable to scan documentation context default
com.google.common.util.concurrent.UncheckedExecutionException: java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class springfox.documentation.spi.service.contexts.SecurityContext (java.util.ArrayList is in module java.base of loader 'bootstrap'; springfox.documentation.spi.service.contexts.SecurityContext is in unnamed module of loader 'app')
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2214)
    at com.google.common.cache.LocalCache.get(LocalCache.java:4053)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:4057)
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4986)
    at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4992)
    at springfox.documentation.spring.web.scanners.CachingOperationReader.read(CachingOperationReader.java:57)
    at springfox.documentation.spring.web.scanners.ApiDescriptionReader.read(ApiDescriptionReader.java:70)
    at springfox.documentation.spring.web.scanners.ApiListingScanner.scan(ApiListingScanner.java:134)
    at springfox.documentation.spring.web.scanners.ApiDocumentationScanner.scan(ApiDocumentationScanner.java:71)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.scanDocumentation(DocumentationPluginsBootstrapper.java:101)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167)
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)
    at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)
    at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)
    at java.base/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:144)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:782)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:774)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:339)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1340)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1329)
    at com.example.emos.wx.EmosWxApiApplication.main(EmosWxApiApplication.java:16)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class springfox.documentation.spi.service.contexts.SecurityContext (java.util.ArrayList is in module java.base of loader 'bootstrap'; springfox.documentation.spi.service.contexts.SecurityContext is in unnamed module of loader 'app')
    at springfox.documentation.spi.service.contexts.OperationContext$1.apply(OperationContext.java:94)
    at com.google.common.collect.Iterators$6.computeNext(Iterators.java:617)
    at com.google.common.collect.AbstractIterator.tryToComputeNext(AbstractIterator.java:145)
    at com.google.common.collect.AbstractIterator.hasNext(AbstractIterator.java:140)
    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.spi.service.contexts.OperationContext.securityContext(OperationContext.java:90)
    at springfox.documentation.swagger.readers.operation.OperationAuthReader.apply(OperationAuthReader.java:58)
    at springfox.documentation.spring.web.plugins.DocumentationPluginsManager.operation(DocumentationPluginsManager.java:120)
    at springfox.documentation.spring.web.readers.operation.ApiOperationReader.read(ApiOperationReader.java:73)
    at springfox.documentation.spring.web.scanners.CachingOperationReader$1.load(CachingOperationReader.java:50)
    at springfox.documentation.spring.web.scanners.CachingOperationReader$1.load(CachingOperationReader.java:48)
    at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3628)
    at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2336)
    at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2295)
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2208)
    ... 31 common frames omitted

  • 写回答

4条回答 默认 最新

  • 怀夢 2021-05-20 12:20
    关注
    package com.example.emos.wx.config;
    
    import io.swagger.annotations.ApiOperation;
    import org.springframework.context.annotation.Bean;
    
    import org.springframework.context.annotation.Configuration;
    
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.ApiKey;
    import springfox.documentation.service.AuthorizationScope;
    import springfox.documentation.service.SecurityReference;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spi.service.contexts.SecurityContext;
    import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        @Bean
        public Docket createRestApi(){
            Docket docket=new Docket(DocumentationType.SWAGGER_2);
            //配置Docket基本信息
            ApiInfoBuilder builder=new ApiInfoBuilder();
            builder.title("EMOS在线办公");
            ApiInfo info=builder.build();
            docket.apiInfo(info);
    
            //哪些类的哪些方法要添加到swagger里面
            ApiSelectorBuilder selectorBuilder=docket.select();
            selectorBuilder.paths(PathSelectors.any());
            selectorBuilder.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class));
            selectorBuilder.build();
            ApiKey apikey=new ApiKey("token","token","header");
    
            List<ApiKey> list=new ArrayList<>();
            list.add(apikey);
            docket.securitySchemes(list);
            //设定令牌作用域
            AuthorizationScope scope=new AuthorizationScope("global","accessEverything");
            AuthorizationScope[] scopes={scope};
            SecurityReference reference=new SecurityReference("token",scopes);
            List refList=new ArrayList();
            refList.add(reference);
            SecurityContext context=SecurityContext.builder().securityReferences(refList).build();
            List ctxList=new ArrayList();
            ctxList.add(ctxList);
            docket.securityContexts(ctxList);
            return docket;
        }
    }
    
    

    上面是swagger的配置类,下面是主启动类,卡我好几天了奇怪的很

    package com.example.emos.wx;
    
    
    
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    
    
    @ServletComponentScan
    @SpringBootApplication
    public class EmosWxApiApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EmosWxApiApplication.class, args);
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档