什么是Swagger
- Swagger 能够生成一个具有互动性的API控制台,开发者能够用来疾速进修和尝试API
- Swagger 能够生成客户端SDK代码用于种种差别的平台上的完成
- Swagger 文件能够在很多差别的平台上从代码解释中自动生成
- Swagger 有一个壮大的社区
依靠导入
<!-- Swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency>
到场设置
swagger: title: 项目 API description: SpringBoot 集成 Swagger 项目 API version: 1.0 terms-of-service-url: http://www.baidu.com/ base-package: cn.anothertale.springbootshiro # 这一项指定须要生成 API 的包,平常就是 Controller contact: name: taohan url: http://www.baidu.ccom/ email: 1289747698@qq.com
竖立 Swagger Config
package cn.anothertale.springbootshiro.config.swagger; import lombok.Getter; import lombok.Setter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; 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; /** * description: swagger 设置中间 * * @author: taohan * @date: 2019年03月20日 * @time: 16:52 */ @Getter @Setter @Configuration @EnableSwagger2 @ConditionalOnClass(EnableSwagger2.class) @ConfigurationProperties(prefix = "swagger") public class SwaggerConfig { /** * API 接口包途径 */ private String basePackage; /** * API 页面题目 */ private String title; /** * API 形貌 */ private String description; /** * 效劳条目地点 */ private String termsOfServiceUrl; /** * 版本号 */ private String version; /** * 联系人 */ private Contact contact; @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage(basePackage)) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title(title) .description(description) .termsOfServiceUrl(termsOfServiceUrl) .version(version) .contact(contact) .build(); } }
经由过程注解标明 API
Swagger 默许依据设置的包,扫描一切接口并生成对应的 API 形貌和参数信息。
经常使用注解及对应属性以下:
@Api (形貌一个 API 类,标注在 Controller 上)
- value:url 的途径值
- tags:假如设置该值,value 的值会被掩盖
- description:API 的资本形貌
- basePath:基础途径能够不设置
- produces:比方:application/json, application/xml 相似 RequestMapping 对应属性
- consumes:比方:application/json, application/xml
- authorizations:高等特征认证时设置
- hidden:是不是在文档中隐蔽
@ApiOperation (用在 Controller 要领上,申明要领的作用)
- value:url 的途径值
- tags:假如设置该值,value 的值会被掩盖
- description:对 API 资本的形貌
- basePath:基础途径能够不设置
- position:假如设置多个 API 想转变展现位置,可经由过程该属性设置
- response:返回的对象
- responseContainer:这些对象是有用的 List、Set、Map,其他无效
- httpMethod:要求体式格局
- code:HTTP 状况码,默许200
- extensions:扩大属性
@ApiImplicitParams (用在 Controller 要领上,形貌一组要求参数)
- value:ApiImplicitParam 数组,见下一注解
- value:ApiImplicitParam 数组,见下一注解
@ApiImplicitParam(形貌一个要求参数)
- name:参数称号
- value:参数值
- defaultValue:参数默许值
- required:是不是必需,默许 false
- access:不过量形貌
- example:示例
@ApiResponses (形貌一组相应)
- value:ApiResponse数组,见下一注解
- value:ApiResponse数组,见下一注解
@ApiResponse (形貌一个相应)
- code:HTTP 的状况码
- message:形貌音讯
末了,能够在浏览器中输入 http://localhost:8080/swagger-ui.html 即可接见!
以上就是SpringBoot 集成 Swagger的要领引见(附代码)的细致内容,更多请关注ki4网别的相干文章!