按照此处的说明操作:
http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
我将这些依赖项添加到我的项目中:
compile "io.springfox:springfox-swagger2:2.7.0"
compile "io.springfox:springfox-swagger-ui:2.7.0"
Run Code Online (Sandbox Code Playgroud)
并像这样配置 SpringFox 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();
}
}
Run Code Online (Sandbox Code Playgroud)
但是 Swagger UI 似乎没有启用。我试过:
我得到的只是:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Sep 11 09:43:46 BST 2017
There was an …Run Code Online (Sandbox Code Playgroud) 我有一个使用嵌入式tomcat服务器运行的spring启动应用程序.我部分成功地将springfox-swagger与应用程序集成在一起.如果我执行/ v2/api-docs,我可以在webapp中查看所有api的所有文档.但是,当我尝试从UI访问相同时,它不起作用.以下是详细结果.
输出 - localhost:8080/api/swagger-resources
[ {
"name" : "default",
"location" : "/v2/api-docs",
"swaggerVersion" : "2.0"
} ]
Run Code Online (Sandbox Code Playgroud)
输出 - localhost:8080/api/v2/api-docs
I get valid results. I can confirm that and the output is too large to paste here
Run Code Online (Sandbox Code Playgroud)
但是当我尝试访问swagger-ui时,它不起作用.下面是我调用以访问swagger-ui的不同URL.
http://localhost:8080/swagger-ui.html - UI is loading, but no documentation of API's is present
http://localhost:8080/api/swagger-ui.html - 404 Not Found
http://localhost:8080/springfox - 404 Not Found
http://localhost:8080/api/springfox - 404 Not Found
Run Code Online (Sandbox Code Playgroud)
下面是我的SwaggerConfig.java类
package com.vmware.vrack.lcm;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import …Run Code Online (Sandbox Code Playgroud) 我无法让它发挥作用。我已经从一个工作的 SpringBoot (v2.2.2) 应用程序和一个工作的 @RestController 开始。为了让 springdoc 工作,我在我的 pom 中包含了以下依赖项:
\n\n <dependency>\n <groupId>org.springdoc</groupId>\n <artifactId>springdoc-openapi-ui</artifactId>\n <version>1.2.28</version>\n</dependency>\nRun Code Online (Sandbox Code Playgroud)\n\n然后我构建并开始了我的服务。然后我浏览到 api-docs 端点,得到:
\n\n{"openapi":"3.0.1","info":{"title":"OpenAPI definition","version":"v0"},"servers":[{"url":"http://localhost:8081","description":"Generated server url"}],"paths":{},"components":{}}\nRun Code Online (Sandbox Code Playgroud)\n\n我是不是少了一步?我假设在运行时它会扫描我的代码,找到 @RestController,找到 @RequestMapping 方法并从中生成 api。
\n\n作为第二次尝试,我决定使用 swagger @Operation 注释来注释我的方法之一,但仍然没有被拾取。
\n\n最后,我尝试使用 springdoc.packagesToScan 手动指定控制器的包,但这也不起作用。
\n\n这感觉就像 springdoc 没有看到我的控制器..即使 spring 看到了..它完全活跃并且可以工作..
\n\n不知道我错过了什么..
\n\n这是我的项目依赖项:
\n\n[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.2.2.RELEASE:compile\n[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.2.2.RELEASE:compile\n[INFO] | | +- org.springframework.boot:spring-boot:jar:2.2.2.RELEASE:compile\n[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.2.2.RELEASE:compile\n[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.2.RELEASE:compile\n[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile\n[INFO] | | | | \\- ch.qos.logback:logback-core:jar:1.2.3:compile\n[INFO] | …Run Code Online (Sandbox Code Playgroud) 我正在为我的应用程序使用 spring 数据休息。
当我在存储库中添加此方法时,出现以下错误并且应用程序无法启动:-
方法:-
@Modifying
@Transactional
@Query("from employee as ft where ft.company.id = ?1")
void deleteAllEmployeeCompany(
@Param("companyId") @RequestParam("companyId") int companyId);
Run Code Online (Sandbox Code Playgroud)
错误:-
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:176) ~[spring-context-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:51) ~[spring-context-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:346) ~[spring-context-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:149) ~[spring-context-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:112) ~[spring-context-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:874) ~[spring-context-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) ~[spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.1.RELEASE.jar!/:1.4.1.RELEASE]
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?
更新:-
所有依赖项的列表:-
[INFO] com.test:test-service:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:1.4.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:1.4.1.RELEASE:compile
[INFO] | | …Run Code Online (Sandbox Code Playgroud) 我使用 swagger 2 创建了一个 Spring Boot 3 的示例。
运行应用程序后,我尝试通过此 URL ( http://localhost:8080/swagger-ui.html) 打开 swagger ui,但得到“Whitelabel Error Page”结果。
下面是 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();
}
}
Run Code Online (Sandbox Code Playgroud)
以下是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> …Run Code Online (Sandbox Code Playgroud) spring-boot ×4
swagger ×4
swagger-ui ×4
java ×2
spring ×2
springfox ×2
openapi ×1
spring-data ×1
spring-mvc ×1
springdoc ×1
swagger-2.0 ×1