我创建了一个使用swagger 2的Sping Boot 3的示例。
在我运行应用程序后,我试图通过这个URL(http://localhost:8080/swagger-ui.html
)打开swagger ui,但我得到了“白标签错误页面”的结果。
下面显示的是 Swagger 配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.deprem"))
.paths(PathSelectors.regex("/.*"))
.build().apiInfo(apiEndPointsInfo());
}
private ApiInfo apiEndPointsInfo() {
return new ApiInfoBuilder().title("Title")
.description("Description")
.contact(new Contact("Name and Surname", "", ""))
.license("Apache 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
.version("1.0.0")
.build();
}
}
以下是pom.xml中swagger ui使用的依赖项
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
下面是控制台输出。
以下是日志信息
2023-02-09T01:08:16.232+03:00 INFO 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2023-02-09T01:08:16.232+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected StandardServletMultipartResolver
2023-02-09T01:08:16.232+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected AcceptHeaderLocaleResolver
2023-02-09T01:08:16.232+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected FixedThemeResolver
2023-02-09T01:08:16.233+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@53b689a4
2023-02-09T01:08:16.233+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected org.springframework.web.servlet.support.SessionFlashMapManager@609759ed
2023-02-09T01:08:16.233+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2023-02-09T01:08:16.233+03:00 INFO 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
2023-02-09T01:08:16.239+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : GET "/swagger-ui.html", parameters={}
2023-02-09T01:08:16.245+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2023-02-09T01:08:16.250+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2023-02-09T01:08:16.250+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2023-02-09T01:08:16.254+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
2023-02-09T01:08:16.255+03:00 DEBUG 24272 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2023-02-09T01:08:16.276+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2023-02-09T01:08:16.281+03:00 DEBUG 24272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
下面是我的application.properties文件
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
logging.level.org.springframework.web=DEBUG
logging.level.io.springfox=DEBUG
如何解决此问题?
下面是我的回购:Link
1条答案
按热度按时间z0qdvdin1#
您需要为Sping Boot 3使用不同的依赖项,并将Swagger逻辑升级到v3:
OpenAPI配置示例:
控制器注解示例:
示例DTO: