SpringBoot中如何整合Swagger2呢?
下文笔者讲述SpringBoot中整合Swagger2的方法分享,如下所示
Swagger简介
Swagger用于自动生成Restful接口说明文档工具 在前后端分离时 后端可直接使用swagger生成的文档进行接口调试
Springboot整合swagger2的方法
实现思路:
1.引入maven jar包
2.SwaggerConfigProperties结合application.properties进行相应的配置
通过以上配置后,即可正常使用
例:
添加Swagger2依赖
<!--配置文件解析器,实例化SwaggerConfigProperties,并为各个属性赋值 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!-- Swagger Maven dependency start--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> <!--end-->
SwaggerConfigProperties
package com.java265.swagger.demo.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="swagger.config")
public class SwaggerConfigProperties {
private Boolean enable;
private String packageScan;
private String title;
private String description;
private String version;
public Boolean getEnable() {
return enable;
}
public void setEnable(Boolean enable) {
this.enable = enable;
}
public String getPackageScan() {
return packageScan;
}
public void setPackageScan(String packageScan) {
this.packageScan = packageScan;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
application.properties文件
server.port=8090 swagger.config.enable=true swagger.config.packageScan=com.java265.swagger.demo.controller swagger.config.title=apiTest swagger.config.description=我是一个测试 swagger.config.version=0.8.1
SpringBoot的Swagger2配置类
package com.java265.swagger.demo.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
@EnableConfigurationProperties({ SwaggerConfigProperties.class })
@ComponentScan(basePackageClasses = SwaggerConfigProperties.class)
public class SwaggerConfig extends WebMvcConfigurerAdapter {
@Autowired
private SwaggerConfigProperties scp;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if(scp.getEnable()){
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
@Bean
public Docket createRestApi() {
ApiInfo apiInfo = new ApiInfoBuilder().title(scp.getTitle())//大标题
.description(scp.getDescription()).version(scp.getVersion())//版本
.build();
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo).enable(scp.getEnable())
.useDefaultResponseMessages(false).select()
.apis(RequestHandlerSelectors.basePackage(scp.getPackageScan())).build();
}
}
测试访问 http://localhost:8090/swagger-ui.html
swagger涉及到的注解
@Api(description = “Swagger测试类02”):
用在类上解释说明。标记一个Controller类做为swagger 文档资源,与Controller注解并列使用。
@ApiOperation(“第二个测试接口02”):
用在方法上解释说明
@ApiResponses({ @ApiResponse(code=200,message = “调用成功”),
@ApiResponse(code=500,message=“调用失败”)}):
用在方法上解释说明响应状态码
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


