整个项目还是比较大的,测试起来很麻烦,通过swagger来进行集中测试就会方便很多,浏览器中deletemapping等不能测试,可使用swagger进行测试
下面介绍在我的项目中整合swagger的步骤
1.创建swagger路径
因为项目中用到很多common模块,那干脆将swagger放到common中,这样就会很方便的管理调用,结构目录如下

2.在Swagger中添加相应的代码
@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("Helen", "http://atguigu.com", "55317332@qq.com"))
.build();
}
}
4.在service中引用

因为是在TeacherController类中写的方法所以,属于最外层是service中,所以在service的pom中加上common的依赖
<dependency>
<groupId>com.zt</groupId>
<artifactId>service_base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
还要在启动类中添加包扫描,common的包路径与service中的包路径相同,这样做为了能够扫描到common下的service_base下的内容。
@ComponentScan(basePackages = {"com.zt"})//为了能够扫描到common中service_base中的包
5.使用Swagger测试
使用时重新启动整个项目,swagger默认的测试地址是------localhost:8001/swagger-ui.html(端口号自己设定的)


因为我的删除操作设置了id为通过路径传值@DeleteMapping("{id}"),所以直接在测试环境下Parameter中id输入id,点击try it out即可。
一开始在此页面中没有中文提示,所以通过在方法上设置注解@ApiOperation(value = "逻辑删除 "),方法参数中设置@ApiParam(name = "id",value = "讲师id",required = true),就会出现中文提示
1538

被折叠的 条评论
为什么被折叠?



