Spring Cloud 接入 Swagger 2.9 UI 2.2.2

Spring Cloud 集成Swagger 2.9 ui 2.2.2

  • 新建项目
  • pom加依赖
<code> <dependency>
\t<groupid>io.springfox/<groupid>
\t\t<artifactid>springfox-swagger2/<artifactid>
\t\t<version>2.9.2/<version>
/<dependency>

<dependency>
<groupid>io.springfox/<groupid>
<artifactid>springfox-swagger-ui/<artifactid>
<version>2.2.2/<version>
/<dependency>/<code>
  • ui版本也可以换成2.8.0,我在官方maven库查找的时候,2.9.2也有,但是下载不下来。Maven库地下:https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui
  • 待导入包结束,新建SwaggerConfig.java
  • <code>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("xxx", "http://www.xxx.com", "[email protected]"))
    //版本号
    .version("1.0")
    //描述
    .description("API 描述")
    .build();
    }
    }/<code>
    • 新建TestController.java
    <code>@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;
    }

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

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

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

    body(不常用)
    form(不常用)

    @ApiResponses
    接口返回信息描述/<code>


    分享到:


    相關文章: