2022-7-23
springboot项目整合swagger2项目,在访问swagger-ui.html
页面时候发生错误,如下:
控制台报错为:No mapping for GET /emos-wx-api/swagger-ui.html
解决办法:让swagger的配置类SwaggerConfig
继承WebMvcConfigurer
接口并且实现其中addResourceHandlers
方法,如下:文章来源:https://www.toymoban.com/news/detail-602850.html
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
WebMvcConfigurer.super.addResourceHandlers(registry);
}
@Bean
public Docket createRestApi() {
Docket docket = new Docket(DocumentationType.SWAGGER_2);
//ApiInfoBuilder定义Swagger页面基本信息
ApiInfoBuilder builder = new ApiInfoBuilder();
builder.title("EMOS在线办公系统");
ApiInfo info = builder.build();
docket.apiInfo(info);
//使用ApiSelectorBuilder将Web方法添加到swagger页面
ApiSelectorBuilder selectorBuilder = docket.select();
selectorBuilder.paths(PathSelectors.any());
//加入对应类对应方法具体通过添加注解ApiOperation实现
selectorBuilder.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class));
docket = selectorBuilder.build();
//加入JWT到项目(令牌登录) 实现验证登录成功之后才能调用对应Web方法
ApiKey apiKey = new ApiKey("token", "token", "header");
//List<ApiKey>用户需要输入什么参数 请求提交的参数
List<ApiKey> apiKeyList = new ArrayList<>();
apiKeyList.add(apiKey);
docket.securitySchemes(apiKeyList);
//AuthorizationScope[] JWT认证在Swagger中的作用域
AuthorizationScope scope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] scopes = {scope};
//List<SecurityReference> 令牌的作用域
SecurityReference reference = new SecurityReference("token", scopes);
List refList = new ArrayList();
refList.add(reference);
//List<SecurityContext> 令牌上下文
SecurityContext context = SecurityContext.builder().securityReferences(refList).build();
List cxtList = new ArrayList();
cxtList.add(context);
docket.securityContexts(cxtList);
return docket;
}
}
再次访问http://localhost:8080/项目工程名/swagger-ui.html
,会发现成功访问了
文章来源地址https://www.toymoban.com/news/detail-602850.html
到了这里,关于No mapping for GET /swagger-ui.html的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!