Swagger2介紹
前后端分離開發模式中,api文檔是最好的溝通方式。
Swagger 是一個規范和完整的框架,用於生成、描述、調用和可視化 RESTful 風格的 Web 服務。
及時性 (接口變更后,能夠及時准確地通知相關前后端開發人員)規范性 (並且保證接口的規范性,如接口的地址,請求方式,參數及響應格式和錯誤信息)一致性 (接口信息一致,不會出現因開發人員拿到的文檔版本不一致,而出現分歧)可測性 (直接在接口文檔上進行測試,以方便理解業務)
配置Swagger2
引入相關依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided </scope>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<scope>provided </scope>
</dependency>
<!--lombok用來簡化實體類:需要安裝lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided </scope>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<scope>provided </scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<scope>provided </scope>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X集成redis所需common-pool2
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>-->
</dependencies>
在模塊service-base中,創建swagger的配置類
創建包com.soyoungboy.servicebase.config,創建類SwaggerConfig
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
.paths(Predicates.not(PathSelectors.regex("/admin/.*")))
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("網站-課程中心API文檔")
.description("本文檔描述了課程中心微服務接口定義")
.version("1.0")
.contact(new Contact("Helen", "http://soyoungboy.com", "55317332@qq.com"))
.build();
}
}
在模塊service模塊中引入service-base
<dependency>
<groupId>com.soyoungboy</groupId>
<artifactId>service-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
5、在service-edu啟動類上添加注解,進行測試
@SpringBootApplication
@ComponentScan(basePackages = {"com.soyoungboy"})
public class EduApplication {
public static void main(String[] args) {
SpringApplication.run(EduApplication.class, args);
}
}
API模型
可以添加一些自定義設置,例如:
定義樣例數據
@ApiModelProperty(value = "創建時間", example = "2019-01-01 8:00:00")
@TableField(fill = FieldFill.INSERT)
private Date gmtCreate;
@ApiModelProperty(value = "更新時間", example = "2019-01-01 8:00:00")
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;
定義接口說明和參數說明
定義在類上:@Api
定義在方法上:@ApiOperation
定義在參數上:@ApiParam
@Api(description="講師管理")
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherAdminController {
@Autowired
private TeacherService teacherService;
@ApiOperation(value = "所有講師列表")
@GetMapping
public List<Teacher> list(){
return teacherService.list(null);
}
@ApiOperation(value = "根據ID刪除講師")
@DeleteMapping("{id}")
public boolean removeById(
@ApiParam(name = "id", value = "講師ID", required = true)
@PathVariable String id){
return teacherService.removeById(id);
}
}