一: swagger 是什么?
1, 是一款让你更好的书写 API 文档的规范且完整框架.
2, 提供描述, 生产, 消费和可视化 RESTful web Service.
3, 是由庞大工具集合支撑的形式化规范. 这个集合涵盖了从终端用户接口, 底层代码库到商业 API 管理的方方面面.
方法一: 使用第三方依赖 (最简单的方法)
1, 在 pom.xml 文件中添加第三方 swagger 依赖 ()
- <dependency>
- <groupId>com.spring4all</groupId>
- <artifactId>swagger-spring-boot-starter</artifactId>
- <version>1.7.0.RELEASE</version>
- </dependency>
- View Code
2, 在 Spring Boot 项目的启动类上添加 @EnableSwagger2Doc 注解, 就可以直接使用啦.
3, 这是 GitHub 上这个 swagger 依赖实现的项目, 里面有详细的讲解.
方法二: 使用官方依赖
1, 在 pom.xml 文件中添加 swagger 相关依赖
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>${springfox-version}</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>${springfox-version}</version>
- </dependency>
- View Code
第一个是 API 获取的包, 第二是官方给出的一个 ui 界面. 这个界面可以自定义, 默认是官方的, 对于安全问题, 以及 ui 路由设置需要着重思考.
2,swagger 的 configuration
需要特别注意的是 swagger scan base package, 这是扫描注解的配置, 即你的 API 接口位置.
- @Configuration
- @EnableSwagger2
- public class Swagger2 {
- @Bean
- public Docket createRestApi() {
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo())
- .select()
- .apis(RequestHandlerSelectors.basePackage("com.yss.ms.admin"))
- .paths(PathSelectors.any())
- .build();
- }
- private ApiInfo apiInfo() {
- return new ApiInfoBuilder()
- .title("服务: 发布为 daocke 镜像, 权限管理, 用户管理, 页面管理, 日志 后台 APIs")
- .description("服务: 发布为 daocke 镜像, 权限管理, 用户管理, 页面管理, 日志 后台")
- .termsOfServiceUrl("http://192.168.103.198:10070/platformgroup/ms-admin")
- .contact("程序猿 DD")
- .version("1.0")
- .build();
- }
- }
- View Code
三, 具体使用
1, 在 API 上做一些声明
- @RequestMapping("/swagger/test/map")
- @ApiOperation(value="xxxx", tags = "xxxx")
- @ApiImplicitParams({
- @ApiImplicitParam(name="name", dataType = "string", value = "only return models...")
- })
- @ApiResponses(value = {
- @ApiResponse(code = 200, message="Indicates ..."),
- @ApiResponse(code = 404, message = "not found error")
- })
- public Result<String> testSwaggerMap(Map map){
- return null;
- }
- View Code
来源: http://www.bubuko.com/infodetail-2775461.html