使用 swagger2 时候遇到的问题

1. 启动swagger 报错 Unable to infer base url. This is common when using dynamic…

Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. 
The base url is the root of where all the swagger resources are served.
 For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/.
  Please enter the location manually:

swagger 2.7 及之前的版本好像不会出现这种问题.

原因可能 :
  • Swagger2 版本 落后与 身份验证版本 (SpringSecurity),更新到Springfox 2.8.0后,/swagger-resources/**路由不会被Authorization标头调用
    • 解决方法
    在身份认证(SpringSecurity)的时候添加 
    .antMatchers("/swagger-resources/**").permitAll()
    
  • 被类似shiro的权限控制组件拦截
    • 解决方法
    过滤swagger页面的权限控制
     /swagger-ui.html
     /webjars/**
     /swagger-resources/**
     /v2/**
    
  • 没有增加@EnableSwagger2 启用swagger的配置
    • 解决方法
    SwaggerConfig 上增加 @EnableSwagger2 ,启用swagger的配置
    
  • 以上信息都配置,仍然报错。经测试:@EnableSwagger2 配置更换到启动类上可正常访问
    说明这不是肯定不是根本原因。比对配置文件发现SwaggerConfig类上的注解
    @ConditionalOnProperty(prefix = “xxx”, name = “yyy”, havingValue = “true”)被修改
    @ConditionalOnProperty注解控制此类上的其他注解是否生效
    • 解决方法
    修改配置文件中的xxx.yyy和配置文件(配置中心)的一致
    
2. 项目启动正常,进入swagger 界面前端正常,但是后端报错
2020-03-06 18:25:30.353 ERROR 3012 --- [nio-8080-exec-5] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-5] Exception processing template "login": Error resolving template [login], template might not exist or might not be accessible by any of the configured Template Resolvers

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [login], template might not exist or might not be accessible by any of the configured Template Resolvers
	at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	......
	at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189) [thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373) [spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	......
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) [tomcat-embed-core-9.0.31.jar:9.0.31]
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) [spring-webmvc-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) [tomcat-embed-core-9.0.31.jar:9.0.31]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-9.0.31.jar:9.0.31]
	......
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.31.jar:9.0.31]
	......
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]
	......
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.31.jar:9.0.31]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]

2020-03-06 18:25:30.356 ERROR 3012 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template [login], template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [login], template might not exist or might not be accessible by any of the configured Template Resolvers
	at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	......
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.31.jar:9.0.31]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
原因 :

Swagger2 版本 与 sprinboot 项目版本不适配,springboot 项目版本为 2.2.5 , Swagger2 版本为 2.9.2

解决方法 :
将 Swagger2 版本降为 2.8.0

参考:swagger2使用遇到的坑

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章