美文网首页Java学习笔记JavaEE 学习专题Spring Boot
Spring Boot + swagger2 整合自动生成RES

Spring Boot + swagger2 整合自动生成RES

作者: FlySheep_ly | 来源:发表于2017-10-09 09:56 被阅读476次

    一、添加Swagger2依赖

        <properties>
            <springfox.version>2.7.0</springfox.version>
        </properties>
    
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>${springfox.version}</version>
            </dependency>
    
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>${springfox.version}</version>
            </dependency>
    

    二、编写Swagger2配置类

    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.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    /**
     * Created by flysheep on 2017/9/30.
     */
    @Configuration
    @EnableSwagger2
    public class Swagger2Configuration {
    
        @Bean
        public Docket buildDocket() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(buildApiInf())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo buildApiInf() {
            return new ApiInfoBuilder()
                    .title("security-login-check 接口文档")
                    .description("security-login-check 提供的接口描述文档")
                    .build();
    
        }
    
    }
    

    三、编写Controller

    import com.domain.base.ResultDTO;
    import com.domain.request.PasswordErrorFrequencyRequest;
    import com.util.ResultDTOUtil;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * Created by flysheep on 2017/9/30.
     */
    @Api(value = "安全登录校验controller", description = "密码错误次数校验", tags = {"安全登录校验接口"})
    @RestController
    public class SecurityLoginCheckController {
    
        @ApiOperation("密码错误次数校验")
        @PostMapping("/checkPassword")
        public ResultDTO checkPasswordErrorFrequency(PasswordErrorFrequencyRequest request) {
            // TODO: 2017/9/30 具体的业务逻辑
            return ResultDTOUtil.successResult(null);
        }
    
    }
    

    完成上述代码后,打包Spring Boot程序并启动,打开浏览器访问:http://localhost:8080/swagger-ui.html,就能看到展示的RESTful API的页面。

    image.png

    相关文章

      网友评论

        本文标题:Spring Boot + swagger2 整合自动生成RES

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