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;
}

}
 
 
 
 
 
 
 
 
 
 
 
 
 
posted @   sensen~||^_^|||&  阅读(291)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示