Swagger使用案例
1、配置Swagger2
swagger作用:生成一个在线的文档,在这个文档里可以方便接口的测试。
1.1、编写swagger配置类
package com.nonglin.baseservice;
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 //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", "1123@qq.com"))
.build();
}
}
6.2、引入依赖
<!--引入模块service_base-->
<dependency>
<groupId>com.nonglin</groupId>
<artifactId>service_base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
6.3、添加注解
package com.nonglin.eduservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* @Description :
* @Author :lenovo
* @Date :2021/2/21 18:59
*/
@SpringBootApplication
@ComponentScan(basePackages = {"com.nonglin"})
public class EduApplication {
public static void main(String[] args) {
SpringApplication.run(EduApplication.class,args);
}
}
6.4、浏览器地址栏访问:http://localhost:8001/swagger-ui.html
6.5、定义接口说明和参数说明
定义在类上:@Api
定义在方法上:@ApiOperation
定义在参数上:@ApiParam
package com.nonglin.eduservice.controller;
import com.nonglin.eduservice.entity.EduTeacher;
import com.nonglin.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* <p>
* 讲师 前端控制器
* </p>
*
* @author Lenovo
* @since 2021-02-21
*/
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
//把service注入
@Autowired
private EduTeacherService eduTeacherService;
//查询所有讲师信息
@ApiOperation(value = "所有讲师列表")
@GetMapping("/findAll")
public List<EduTeacher> findAll() {
List<EduTeacher> list = eduTeacherService.list(null);
return list;
}
//删除讲师
@ApiOperation(value = "根据ID删除讲师")
@DeleteMapping("{id}")
public boolean deleteTeacher( @ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {
boolean flag = eduTeacherService.removeById(id);
return flag;
}
}