无客户端JHipster应用无法访问Swagger UI,求解决方案
我创建了一个无客户端的JHipster应用,初始配置选项如下:
? Which *type* of application would you like to create? Monolithic application (recommended for simple projects) ? What is the base name of your application? nouisample ? Do you want to make it reactive with Spring WebFlux? No ? What is your default Java package name? com.mycompany.myapp ? Which *type* of authentication would you like to use? JWT authentication (stateless, with a token) ? Which *type* of database would you like to use? SQL (H2, PostgreSQL, MySQL, MariaDB, Oracle, MSSQL) ? Which *production* database would you like to use? MySQL ? Which *development* database would you like to use? MySQL ? Which cache do you want to use? (Spring cache abstraction) No cache - Warning, when using an SQL database, this will disable the Hibernate 2nd level cache! ? Would you like to use Maven or Gradle for building the backend? Maven ? Do you want to use the JHipster Registry to configure, monitor and scale your application? No ? Which other technologies would you like to use? API first development using OpenAPI-generator ? Which *Framework* would you like to use for the client? No client ? Would you like to enable internationalization support? No ? Please choose the native language of the application English ? Besides JUnit and Jest, which testing frameworks would you like to use? ? Would you like to install other generators from the JHipster Marketplace? No
应用启动时激活了以下配置:
2023-06-18T15:15:58.305+05:30 INFO 23300 --- [ restartedMain] com.mycompany.myapp.NouisampleApp : The following 2 profiles are active: "dev", "api-docs"
应用运行在http://localhost:8080/,但无法访问Swagger UI,尝试过以下URL均返回404错误:
- http://localhost:8080/#/docs
- http://localhost:8080/swagger-ui.html
- http://127.0.0.1:8761/swagger-ui/index.html
错误页面内容:
Your request cannot be processed Sorry, an error has occurred. Status: Not Found (Not Found) Message: Not Found
请问如何才能访问Swagger UI?
针对无客户端的JHipster应用,Swagger UI的访问逻辑和带客户端的应用存在差异,结合你的配置情况,可按以下步骤排查解决:
使用正确的Swagger UI访问路径
无客户端JHipster应用采用SpringDoc替代旧版Swagger,默认访问路径为:http://localhost:8080/swagger-ui/index.html
你之前尝试的swagger-ui.html是旧版路径,新版本已不再使用。确认
api-docs配置有效性
检查application-dev.yml或application-api-docs.yml中是否包含SpringDoc的启用配置:springdoc: api-docs: enabled: true swagger-ui: enabled: true若配置缺失,添加后重启应用。
验证API文档生成状态
先访问http://localhost:8080/v3/api-docs,如果能返回JSON格式的API文档,说明文档生成正常,问题仅在UI路径;若也返回404,需检查:pom.xml中是否引入springdoc-openapi-starter-webmvc-ui依赖- 启动日志是否确认
api-docsprofile已正确激活
放行Swagger相关路径的JWT认证
由于你使用JWT认证,需确保Swagger UI和API文档路径被加入白名单。修改SecurityConfiguration.java的configure(HttpSecurity http)方法,添加以下规则:http.authorizeHttpRequests() .requestMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll() // 保留其他原有认证规则该配置允许未认证用户访问Swagger相关资源。
无效路径说明
你尝试的http://127.0.0.1:8761/swagger-ui/index.html是JHipster Registry的访问路径,但你创建应用时已选择不使用Registry,因此该路径完全无效,无需尝试。
内容的提问来源于stack exchange,提问作者Sanal M

