要文檔化Spring Boot Endpoints,我們建議使用Swagger
在pom.xml
中添加以下依賴項:
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency><dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
在項目中創建一個新的Java類,例如SwaggerConfig.java
,然后添加以下代碼:
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
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.basePackage("com.example.yourapp"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(
"Your App Title",
"Your App Description",
"API Version",
"Terms of Service URL",
new Contact("Name", "URL", "Email"),
"License Name",
"License URL",
Collections.emptyList()
);
}
}
在application.properties
文件中添加以下內容:
springfox.documentation.swagger.v2.path=/api-docs
或者在application.yml
中添加以下內容:
springfox:
documentation:
swagger:
v2:
path: /api-docs
啟動你的Spring Boot應用程序,然后在瀏覽器中訪問以下URL:
http://localhost:8080/swagger-ui.html
這將顯示Swagger UI,您可以在其中查看和測試您的Spring Boot Endpoints。
注意:請確保將"com.example.yourapp"替換為您自己的基本包名稱。