如何在项目当中集成Swagger-API开发

目录

1.介绍

2.应用


1.介绍

 

  • 关于“介绍”最具有权威性,不用多说一定是官方了,Swagger官网地址:https://swagger.io/ 结。
  • 对Swagger官方从不同角度如:设计、开发、文档、测试、监控、规范等等。

2.应用

  • 添加maven依赖

  
        
            io.springfox
            springfox-swagger2
            2.2.2
        
        
            io.springfox
            springfox-swagger-ui
            2.2.2
        

 

  • 配置类
package com.swagger.test.config;

import io.swagger.annotations.Api;
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.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Configuration {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                 //  加了api注解Controller
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 接口文档头
                .title("Swagger接口文档")
                // 接口文档描述
                .description("Swagger相关接口的文档")
                // 服务器地址
                .termsOfServiceUrl("http://www.SwaggerTest.com")
                // 项目版本号
                .version("1.0")
                .build();
    }

}
  • 测试接口
    package com.swagger.test.controller;
    
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiImplicitParam;
    import io.swagger.annotations.ApiImplicitParams;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    /**
     * 测试Swaager2接口
     *
     * @author: @xxx
     * @create: 2019-07-02 15:53
     */
    @Api(value = "TestSwaager2Controller",description = "test接口")
    @Controller
    public class TestSwaager2Controller {
    
    
        @ApiOperation(value = "这是一个测试Swaager接口实例,好好验证哦。不然")
        @ApiImplicitParams({@ApiImplicitParam(name = "name", dataType = "String", required = true,paramType = "query"),
                @ApiImplicitParam(name = "desc", dataType = "String", required = true,paramType = "query")
        })
        @ResponseBody
        @RequestMapping(value = "/swaager/swaagerTest", method = RequestMethod.POST)
        public String Test(String name, String desc) {
            return "这是一个测试Swaager接口实例-----------{"+String.format("%s",name)+"},----------{"+String.format("%s",desc)+"}";
    
        }
    
    }

    最后我们在浏览器中输入地址 http://localhost:8000/swagger-ui.html,OK就是这样

    如何在项目当中集成Swagger-API开发_第1张图片

 总结:后面学习一些Swagger常用注解基本用法、描述,并会记录在我的博客当中,“锲而不舍,金石可镂”。

你可能感兴趣的:(Swagger,Swagger)