美文网首页SpringBoot极简教程 · Spring Boot
Spring Boot 整合 swagger和swagger-u

Spring Boot 整合 swagger和swagger-u

作者: zhangweisep | 来源:发表于2020-03-17 15:01 被阅读0次

    pom.xml配置依赖

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

    SwaggerConfig配置

    package com.demo.config;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.ParameterBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.schema.ModelRef;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Parameter;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Filename: SwaggerConfig
     * Author:   Zhang Wei
     * Date:     2018/8/28 15:02
     * Description:日志配置类
     * History:
     */
    @Slf4j
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        
        @Bean
        public Docket createRestApi() {
            ParameterBuilder parameterBuilder = new ParameterBuilder();
            List<Parameter> parameters       = new ArrayList<>();
            parameterBuilder.name("Authorization").description("Authorization")
                    .modelRef(new ModelRef("string")).parameterType("header")
                    .required(false).build();
            parameters.add(parameterBuilder.build());
    
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.demo.controller"))
                    .paths(PathSelectors.any())
                    .build()
                    .globalOperationParameters(parameters)
                    .apiInfo(apiInfo());
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("demo")
                    .description("模块")
                    .version("1.0")
                    .build();
        }
    }
    

    使用示例

    /**
         * 退出登录
         *    @ApiOperation : swagger注解
         * @param userJson
         * @return
         * @throws Exception
         */
        @ApiOperation(value = "退出登录", notes = "param:UserJson")
        @RequestMapping(value = "/logout", method = RequestMethod.POST)
        public Object logout(@RequestBody UserJson userJson) throws Exception{
            if(EmptyUtil.isEmpty(userJson) || EmptyUtil.isEmpty(userJson.getToken())){
                return ResultUtil.error(ExceptionEnum.PARAM_NULL);
            }
            Result result = userService.logout(userJson);
            return result;
        }
    

    相关文章

      网友评论

        本文标题:Spring Boot 整合 swagger和swagger-u

        本文链接:https://www.haomeiwen.com/subject/nsjtyhtx.html