SwaggerSwagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
1.Swagger配置类:
package com.atguigu.servicebase;
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 //swagger配置类
@EnableSwagger2 //swagger注解,说明它做的是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("Helen", "http://atguigu.com", "55317332@qq.com"))
.build();
}
}
2.Swagger参数说明:
定义在类上:@Api
定义在方法上:@ApiOperation
定义在参数上:@ApiParam
eg:
/**
* <p>
* 讲师 前端控制器
* </p>
*
* @author testjava
* @since 2021-08-11
*/
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
//把service注入进来
@Autowired
private EduTeacherService teacherService;
//1.查询讲师表的所有数据
//访问地址:http://localhost:8080//eduservice/teacher/findAll
@ApiOperation(value = "所有讲师列表") //swagger里的解释说明。
@GetMapping("/findAll") //rest风格
private List<EduTeacher> findAllteacher(){
List<EduTeacher> teachers = teacherService.list(null); //调用service的方法查询所有teacher。
return teachers;
}
// //2.物理删除
// @DeleteMapping("{id}")
// private boolean deleteTeacher(@PathVariable String id){
// return teacherService.removeById(id);
// }
//2.逻辑删除讲师的方法
@ApiOperation(value = "根据ID逻辑删除讲师") //swagger里的解释说明。
@DeleteMapping("{id}") //id值需要通过路径进行传递
public boolean removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){ //@ApiParam(name = "id", value = "讲师ID", required = true) 是swagger里的解释说明,required = true表示必填。
boolean flag = teacherService.removeById(id);
return flag;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示