整合swagger进行接口测试:首先我们创建一个公共模块:以一个maven项目创建.接下来引入依赖:
io.springfox
springfox-swagger2
provided
io.springfox
springfox-swagger-ui
provided
并且在需要用到的启动类中加上注解:
改一下类型为pom,这里不写代码
* 讲师 前端控制器 *
* * @author qiuzhikang * @since 2020-07-23 */ @Api(description = '讲师管理') @RestController @RequestMapping('/eduservice/eduteacher') public class EduTeacherController { @Autowired private EduTeacherService teacherService; //查询所有的老师的信息,rest风格 @ApiOperation('所有讲师列表') @GetMapping('findAll') public List接下来再建一个模块做为common的子模块service_bas创建包,结构如下:编写SwaggerConfig代码
package com.qiu.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 //配置类
@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://71953689.top', '71953689@qq.com'))
.build();
}
}
由于我们的Swager并不在同一个模块中,这么做就是好做一个全局的Swagger,所以我们可以将需要用到Swagger的地方进行配置即可比如说我们在需要引入的依赖中引入
com.qiu
service_base
0.0.1-SNAPSHOT
文章为作者独立观点,不代表股票交易接口观点