Spring Cloud Swagger 2.9 UI 2.2.2

Spring Cloud 集成Swagger 2.9 ui 2.2.2
  • 新建项目
  • pom加依赖
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>
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.service.Contact;  
import springfox.documentation.spi.DocumentationType;  
import springfox.documentation.spring.web.plugins.Docket;  
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {  
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 需要展示文档的包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.security"))
                .paths(PathSelectors.any())
                .build();

    }

    //构建api文档的详细信息函数
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("Test测试Swagger")
                //创建人
                .contact(new Contact("Mr.Hao", "http://www.51hsw.com", "409328820@qq.com"))
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }
}
  • 新建TestController.java
@Api(value = "测试swagger2文档")
@RestController
public class TestController {

    @ApiOperation(value = "专用于测试使用", notes = "备注")
    @ApiImplicitParam(name = "name", value = "名称", paramType = "path", required = true, dataType = "String", example = "你爸爸")
    @ApiResponses({
            @ApiResponse(code = 500, message = "内部程序错误"),
            @ApiResponse(code = 400, message = "请求无效"),
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "你懂得")
    })
    @GetMapping("/test")
    public String test(String name) {
        return "我是测试Swagger2" + name;
    }

    @ApiOperation(value = "测试请求", notes = "备注")
    @ApiImplicitParam(name = "name", value = "参数1", paramType = "path", required = true, dataType = "String", example = "你妈妈")
    @ApiResponses({
            @ApiResponse(code = 500, message = "内部程序错误"),
            @ApiResponse(code = 400, message = "请求无效"),
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "你懂得")
    })
    @GetMapping("/test1")
    public String test1(@RequestParam String name) {
        return "我是测试Swagger2" + name;
    }

}
  • 启动程序,根据启动的端口访问
http://localhost:5003/swagger-ui.html  
点击页面的TestController文字
  • 备注
参数介绍
@EnableSwagger2 @Configuration
配置类需要添加的注解,声明为Swagger

@ApiOperation
指定接口方法的说明,如果方法不加此注解,说明默认为方法名称

@ApiImplicitParams @ApiImplicitParam
接口详细描述的参数信息
其中 ApiImplicitParam 为具体的参数,里面的配置 paramType 需要注意:
 paramType代表参数放在哪个地方
 header-->请求参数的获取:@RequestHeader
 query-->请求参数的获取:@RequestParam
 path(用于restful接口)-->请求参数的获取:@PathVariable
 body(不常用)
 form(不常用)

@ApiResponses
接口返回信息描述

郝先生

继续阅读此作者的更多文章