第一步:导入依赖
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!--swagger ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
第二步:swagger2配置
import com.google.common.base.Predicates;
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.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;
@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("项目标题")
.description("项目描述")
.version("1.0")
.contact(new Contact("作者姓名", "网站地址", "作者联系方式"))
.build();
}
}
第三步:使用
@ApiModel(value="EduTeacher对象", description="讲师")
public class EduTeacher implements Serializable {
@ApiModelProperty(value = "讲师ID")
private String id;
@ApiModelProperty(value = "创建时间")
private Date gmtCreate;
}
@Api(tags="讲师管理")
@RestController
@RequestMapping("/teacher")
public class TeacherController
{
@ApiOperation(value = "删除讲师")
@DeleteMapping("deleteTeacher/{id}")
public R deleteTeacher(
@ApiParam(name = "id", value = "讲师ID", required = true)
@PathVariable String id){
boolean flag = teacherService.removeById(id);
return flag ? R.ok():R.error();
}
}