在CentOS系统上设计和实现Swagger API,通常涉及以下几个步骤:
pom.xml文件中添加以下依赖:<dependency>
<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>
注意:上述版本可能会随着时间而变化,请检查最新版本。
SwaggerConfig.java的类: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.demo.controller")) // 替换为你的控制器包路径
.paths(PathSelectors.any())
.build();
}
}
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/hello")
@ApiOperation(value = "获取问候信息", notes = "这是一个简单的问候接口")
public String hello() {
return "Hello, World!";
}
}
启动应用并访问Swagger UI:
http://localhost:8080/swagger-ui.html(端口号可能因配置而异),你应该能够看到Swagger UI界面,其中列出了你的API接口。测试API:
以上步骤是在CentOS系统上设计和实现Swagger API的基本流程。根据你的具体需求,你可能还需要进行更多的配置和自定义。