在Debian系统上实现Swagger接口文档自动生成,通常需要以下几个步骤:
安装必要的软件包:
sudo apt update
sudo apt install default-jdk maven
创建Maven项目:
mvn archetype:generate -DgroupId=com.example -DartifactId=swagger-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd swagger-demo
添加Swagger依赖:
pom.xml
文件,添加Swagger和Swagger UI的依赖项。以下是一个示例配置:<dependencies>
<!-- Swagger dependencies -->
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.1.12</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-models</artifactId>
<version>2.1.12</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-parser</artifactId>
<version>2.0.28</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-ui</artifactId>
<version>3.54.0</version>
</dependency>
</dependencies>
编写Swagger注解:
package com.example;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
@Api(tags = "Example API")
public class ExampleController {
@GetMapping("/hello")
@ApiOperation(value = "Get a hello message", response = String.class)
public String sayHello() {
return "Hello, World!";
}
}
配置Swagger:
package com.example;
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.basePackage("com.example"))
.paths(PathSelectors.any())
.build();
}
}
运行应用程序:
mvn clean install
java -jar target/swagger-demo-1.0-SNAPSHOT.jar
访问Swagger UI:
http://localhost:8080/swagger-ui.html
,你应该能够看到自动生成的Swagger接口文档。通过以上步骤,你可以在Debian系统上实现Swagger接口文档的自动生成。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: 如何确保Debian MongoDB高可用性