Swagger基础快速上手
Swagger:自动生成Restful接口说明文档工具,也可以在前后端分离时,后端进行接口调试
添加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.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=8001
swagger.config.enable=true
swagger.config.packageScan=com.swagger.demo.controller
swagger.config.title=Swagger
swagger.config.description=this is description
swagger.config.version=2.6.1
SpringBoot的Swagger2配置类
package com.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:8001/swagger-ui.html
涉及到的注解:
- @Api(description = “Swagger测试类02”):用在类上解释说明。标记一个Controller类做为swagger 文档资源,与Controller注解并列使用。
- @ApiOperation(“第二个测试接口02”):用在方法上解释说明
- @ApiResponses({ @ApiResponse(code=200,message = “调用成功”),@ApiResponse(code=500,message=“调用失败”)}):用在方法上解释说明响应状态码
- Swagger详细注解传送门:
异常总结:
原因:控制层的RequestMapping存在重复URL,导致无法建立映射关系;
能力有限;第一次写博客。