swagger
简介
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步
在pom.xml中加入Swagger2的依赖
简单使用
配置依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
创建配置类
@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://atguigu.com", "55317332@qq.com"))
.build();
}
}
添加文档内容
@Api(description = “课程分类管理”)
@CrossOrigin//跨域
@RestController
@RequestMapping("/edu/edu-subject")
public class EduSubjectController {
@Autowired
private EduSubjectService subjectService;
//添加课程分类
@ApiOperation(value = "Excel批量导入")
@PostMapping("/add")
public R addSubject(MultipartFile file){
//1 获取上传的excel文件 MultipartFile
//返回错误提示信息
subjectService.importSubjectData(file,subjectService);
//判断返回集合是否为空
return R.ok();
}
@ApiOperation(value = "嵌套数据列表")
@GetMapping("/list")
public R nestedList(){
List<SubjectNestedVo> subjectNestedVoList = subjectService.nestedList();
return R.ok().data("items", subjectNestedVoList);
}
}
最后启动Spring Boot程序,访问:http://localhost:8001/swagger-ui.html
注意:端口号是启动Spring Boot程序默认的或自己配置的