【发布时间】:2022-01-16 02:06:14
【问题描述】:
当我尝试在我的 Spring Boot 应用程序中启用 swagger 时,它显示空白页。 我正在使用网址 https://localhost:8080/context-path/swagger-ui/ 我能够获取 url https://localhost:8080/context-path/v2/api/docs 的 JSON 数据 对于 swagger ui,我不知道哪里出了问题,任何人都可以帮助我解决这个问题。提前致谢。
Swagger 配置文件
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
我在 pom.xml 中添加了依赖项
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1 </version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</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-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
我有这样的控制器
@GetMapping public ResponseEntity<ProjectDto> getAllProjects() { ProjectDto projectDto = new ProjectDto(); try { List<ProjectDto> projectDtos = projectService.getAllProjects(); projectDto.setProjectDtos(projectDtos.stream().collect(Collectors.toSet())); return new ResponseEntity<>(projectDto, HttpStatus.OK); } catch (ProjectNotFoundException exception) { LOGGER.info(exception.getMessage(), exception); projectDto.setErrorMsg(exception.getMessage()); return new ResponseEntity<>(projectDto, HttpStatus.BAD_REQUEST); } catch (Exception exception) { LOGGER.info(exception.getMessage(), exception); projectDto.setErrorMsg("Something went wrong with projects"); return new ResponseEntity<>(projectDto, HttpStatus.INTERNAL_SERVER_ERROR); } }
【问题讨论】:
-
您是否定义了任何控制器?可以分享一下吗?
-
你能把这个添加到问题中吗?在评论中几乎不可能读到这一点。
标签: java spring-boot swagger-ui springfox