官网:https://swagger.io/
快速接入
1、pom文件加入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
2、摘要相关配置
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo())
// 是否开启
.enable(true).select()
// 扫描的路径包
.apis(RequestHandlerSelectors.basePackage("com.llgcollector.controller"))
// 指定路径处理PathSelectors.any()代表所有的路径
.paths(PathSelectors.any()).build().pathMapping("/");
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SpringBoot-Swagger3集成示例")
.description("springboot | swagger")
.contact(new Contact("test", "https://www.test.com", "test@163.com"))
.version("1.0.0")
.build();
}
}
3、编写controller
@Api(tags = "测试类",value = "Test Controller")
@RestController
@RequestMapping("/test")
public class TestController {
private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);
@Resource
private TestRepo testRepo;
@ApiOperation(value = "获取信息",notes = "get Message")
@RequestMapping(value = "/getMessage", method = RequestMethod.GET)
public ApiResponse getMessage(@ApiParam(name = "id",value = "主键",required = true) Long id) {
return new ApiResponse<>(testRepo.getById(id));
}
}
4、开启Swagger
@EnableOpenApi
@SpringBootApplication
public class Application {...
5、浏览器查看效果
https://127.0.0.1:8081/swagger-ui/index.html
6、如果加了拦截器继承了WebMvcConfigurationSupport会导致swagger失效,此时可以将继承WebMvcConfigurationSupport改成实现WebMvcConfigurer接口