zoukankan      html  css  js  c++  java
  • swagger 2.0

    1、引入jar包

            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.7.0</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.7.0</version>
            </dependency>

    2、配置swagger配置文件

    import io.swagger.annotations.Api;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.ParameterBuilder;
    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;
    
    /**
     * @author xiufengd
     * @date 2021/8/10 10:59
     * @description 用于生成API接口文档的配置
     * @note 未来可期
     */
    @Configuration
    @EnableSwagger2
    //@EnableWebMvc  注掉默认的,改成继承WebMvcConfigurationSupport,重写addResourceHandlers,将swagger-ui.html映射出来
    public class SwaggerConfig extends WebMvcConfigurationSupport {
    
        @Bean
        public Docket petApi() {
    
            ParameterBuilder ticketPar = new ParameterBuilder();
    
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                    .apis(RequestHandlerSelectors.basePackage("com.dxf.controller")) //指定提供接口所在的基包
                    .build();
        }
    
        /**
         * 该套 API 说明,包含作者、简介、版本、host、服务URL
         * @return
         */
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("接口文档")
                    .version("0.1")
                    .description("该接口文档暂时只用于前后端调试对接使用,切勿对外暴露!")
                    .build();
        }
        //将swagger-ui.html 映射到项目中可以直接访问
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**").addResourceLocations(
                    "classpath:/static/");
            registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                    "classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations(
                    "classpath:/META-INF/resources/webjars/");
            super.addResourceHandlers(registry);
        }
    
    }

    3、定义返回类型

    package com.xiufengd.utils;
    
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    
    /**
     * @author xiufengd
     * @date 2021/8/13 9:32
     * @description
     * @note 未来可期
     */
    @ApiModel(value="Result返回对象",description="Result返回对象")
    public class Result {
        // 响应业务状态
        @ApiModelProperty(value="状态",name="status",example="200")
        private Integer status;
    
        // 响应消息
        @ApiModelProperty(value="响应消息",name="msg",example="ok")
        private String msg;
    
        // 响应中的数据
        @ApiModelProperty(value="响应数据",name="data",example="[{"floor":1},{"floor":2}]")
        private Object data;
    
        public Integer getStatus() {
            return status;
        }
    
        public void setStatus(Integer status) {
            this.status = status;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        public Object getData() {
            return data;
        }
    
        public void setData(Object data) {
            this.data = data;
        }
    }

    4、在类上注解写到死import io.swagger.annotations.*;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;

    /**
    * @author xiufengd
    * @date 2021/8/6 17:59
    * @description
    * @note 未来可期
    */
    @Api(value = "测试接口类",tags ="测试接口类")
    @Controller("test")
    @RequestMapping("test")
    public class test {

    @ApiOperation(value = "定义测试接口方法")
    @GetMapping("test")
    //定义返回
    @ApiResponses({
    @ApiResponse(code = 200, message = "{"status":200,"msg":"ok","data":"success data"}"),
    @ApiResponse(code = 201, message = "{"status":201,"msg":"ok","data":"error data"}")
    })
    @ResponseBody
    public Result test(@RequestParam @ApiParam("参数名") String param) throws Exception {
    Result result = new Result();
    result.setStatus(200);
    result.setData("success data");
    result.setMsg("ok");
    return result;
    }
    }

    5、shiro放开权限

        <!-- swagger配置 -->
        /swagger-ui.html=anon
        /swagger-resources=anon
        /v2/api-docs=anon
        /webjars/springfox-swagger-ui/**=anon

    6、页面访问

    http://127.0.0.1:8080/swagger-ui.html

  • 相关阅读:
    spring mvc技术
    转 easyUI的iframe子页面操作父页面元素
    DG
    SqlServer数据库分离附加操作
    SqlServer2008系统数据库的作用和特点
    Oracle基础学习记录1.0
    聚集索引与非聚集索引
    苹果官方 Crash文件分析方法 (iOS系统Crash文件分析方法)
    iOS Crash文件的解析
    在同一台电脑上使用两个github账户
  • 原文地址:https://www.cnblogs.com/xiufengd/p/15124905.html
Copyright © 2011-2022 走看看