1.pom文件引入依赖
io.springfox
springfox-swagger2
2.7.0
io.springfox
springfox-swagger-ui
2.7.0
2.创建配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {@Bean
public Docket ccaApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("task")
.genericModelSubstitutes(DeferredResult.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(true)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.xxx.xxx.controller"))
.paths(or(regex("/.*")))//过滤的接口
.build()
.apiInfo(taskApiInfo());
}private ApiInfo taskApiInfo() {
return new ApiInfoBuilder()
.title("测试服务接口列表")//大标题
.version("1.0")//版本
.build();
}
}
其中,.apis(RequestHandlerSelectors.basePackage(“com.xxx.xxx.controller”))配置了要扫描的包。
3.代码中添加注解
@RestController
@RequestMapping("/test")
public class TestController { @Autowired
private TestService testService;
@RequestMapping(path = "testFunc", method = RequestMethod.GET)
public String testFunc(){
System.out.println("---:");
return "Hello!";
}
}
【使用Swagger-UI进行接口测试】4.启动服务,访问swagger-ui
文章图片
@Api用于给类添加描述。
@ApiOperation用于给方法添加描述。
@ApiParam用于给单个参数添加描述。
@ApiImplicitParams用于给方法的所有参数添加描述。跟@ApiParam类似。
推荐阅读
- 面试|我经历的IT公司面试及离职感受(转)
- Asp.net|System.Globalization.DateTimeFormatInfo.InvariantInfo
- 测试|16年资深测试大牛教你三部成为测试架构师
- Android|Android VideoView如何播放RTSP的流
- web技术|项目管理之如何控制项目进度和质量
- android|测量webView页面性能技术方案
- 测试|selenium-grid AND docker化方案
- Java|Jacoco覆盖率工具使用调研