This article brings you an introduction to the method of integrating Swagger with SpringBoot (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
What is Swagger
Dependency import
<!-- 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>
Add configuration
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
Create 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(); } }
Through annotations Mark API
Swagger defaults to scanning all interfaces and generating corresponding API description and parameter information based on the configured package.
Commonly used annotations and corresponding attributes are as follows:
@Api (Describes an API class, marked on the Controller)
##@ApiOperation (used on the Controller method , explain the function of the method)
(used on Controller method , describing a set of request parameters)
(Describe a request parameter)
value:
code:
The above is the detailed content of Introduction to the method of integrating SpringBoot with Swagger (with code). For more information, please follow other related articles on the PHP Chinese website!