1、添加pom
<!--swagger的 pom -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
2、添加swagger的配置类
Configuration /* 声明配置类 */
@EnableSwagger2 // Swagger 的注解
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("java", "http://atguigu.com", "1500810597@qq.com"))
.build();
}
}
3、使用swagger的注解
1、在controller中使用,看注释
@Api(tags = "讲师管理") swagger:描述这个类模块作用
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
@Autowired
private EduTeacherService eduTeacherService;
@ApiOperation(value = "所有讲师列表") swagger:描述方法
@GetMapping("findAll")
public List<EduTeacher> findAllTeacher(){
List<EduTeacher> list = eduTeacherService.list(null);
return list;
}
@DeleteMapping("{id}") /* 表示:这个id需要通过url来传递过来 */
@ApiOperation(value = "逻辑删除") swagger:描述方法
public boolean removeTeacher(@PathVariable String id) {
boolean num = eduTeacherService.removeById(id);
return num;
}
}
2、在javabean中使用,看注释
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
// @ApiModel : swagger对类进行说明,用于参数用实体类接收
@ApiModel(value="EduTeacher对象", description="讲师")
public class EduTeacher implements Serializable {
private static final long serialVersionUID = 1L; // 默认版本号
@ApiModelProperty(value = "讲师ID") //swagger属性的说明或者数据操作更改
@TableId(value = "id", type = IdType.ID_WORKER_STR)//主键策略
private String id;
@ApiModelProperty(value = "排序")
private Integer sort;
@ApiModelProperty(value = "逻辑删除 1(true)已删除, 0(false)未删除")
@TableLogic // 自动填充
private Boolean isDeleted;
}