• 【Spring Boot&&Spring Cloud系列】Spring Boot项目集成Swagger UI


    前言

    Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。

    一、修改pom.xml文件,加入依赖

               <!--swagger-->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.2.2</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.2.2</version>
            </dependency>
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-core-asl</artifactId>
                <version>1.9.13</version>
            </dependency>

    二、添加Swagger配置类

    package com.slp.util;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    import static springfox.documentation.builders.PathSelectors.regex;
    
    /**
     * Created by sangliping on 2017/8/17.
     */
    @Configuration
    @EnableSwagger2
    public class Swaggers {
    
        @Bean
        public Docket swaggerSpringMvcPlugin() {
            ApiInfo apiInfo = new ApiInfo("sample of springboot", "sample of springboot", null, null, null, null, null);
            Docket docket = new Docket(DocumentationType.SWAGGER_2).select().paths(regex("/user/.*")).build()
                    .apiInfo(apiInfo).useDefaultResponseMessages(false);
            return docket;
        }
    
    
            /*private ApiInfo apiInfo() {
                return new ApiInfoBuilder().title("测试API")
                        .description("测试API1")
                        .version("1.0.0")
                        .build();
            }*/
            /* @Bean
                public Docket createRestApi() {
                    return new Docket(DocumentationType.SWAGGER_2)
                            .apiInfo(apiInfo())
                            .select()
                            .apis(RequestHandlerSelectors.basePackage("com.slp.web"))
                            .paths(regex("/user/.*"))
                            .build();
                }
            */
    
    }

    三、编写测试用的Controller类

    package com.slp.controller;
    
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiImplicitParam;
    import io.swagger.annotations.ApiImplicitParams;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    
    /**
     * Created by sangliping on 2017/8/17.
     * Swagger常用注解:
     *    @Api:修饰整个类,描述Controller的作用
     *    @ApiOperation:描述一个类的一个方法,或者说一个接口
     *    @ApiParam:单个参数描述
     *    @ApiModel:用对象来接收参数
     *    @ApiProperty:用对象接收常熟市,描述对象的一个字段
     *    @ApiIgnore:使用该注解时忽略这个API
     *    @ApiResponse:HTTP响应其中一个描述
     *    @ApiResponses:HTTP响应整个描述
     */
    @RestController
    @RequestMapping("/user")
    @Api(value = "Shop")
    public class SpringBootController {
        @ApiOperation(value = "获取helloworld",notes = "简单的Spring boot请求")
        @RequestMapping
        String home (){
            return "HELLO WORLD";
        }
    
        @ApiOperation(value = "获得参数",notes = "根据参数中的classNo和studentName是的字符串相加")
        @ApiImplicitParams({@ApiImplicitParam(name = "classNo",value ="班级编号",required= true,dataType="String")})
        @RequestMapping(value="/class/{classNo}/to/{studentName}",method = RequestMethod.GET)
        String world(@PathVariable("classNo")String classNo,@PathVariable("studentName")String studentName){
          return classNo + " "+studentName;
        }
    
    
    }

    四、Swagger启动

  • 相关阅读:
    EV录屏
    Oracle 游标详解 【转载至CSDN「鱼丸丶粗面」】
    ORACLE查询表数据占用存储空间大小(清理表碎片)
    开源项目
    Shiro 简介(认证、授权、加密、会话管理、与 Web 集成、缓存等)
    OSCHINA 公布 2019 年度最受欢迎中国开源软件
    腾讯开发者手册
    hutool JAVA 工具类
    小程序登录
    微信小程序button组件样式
  • 原文地址:https://www.cnblogs.com/dream-to-pku/p/7381241.html
Copyright © 2020-2023  润新知