zoukankan      html  css  js  c++  java
  • springboot整合swagger笔记

    首先,在pom.xml中添加依赖

    <!--swagger-->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.7.0</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.7.0</version>
            </dependency>

    然后在启动类中添加Swagger2 java配置文件

    package com.example.demomybatis;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    import org.springframework.context.annotation.Bean;
    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;
    
    @SpringBootApplication
    @EnableSwagger2
    @MapperScan("com.example.demomybatis.mapper")
    public class DemoMybatisApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoMybatisApplication.class, args);
        }
    
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demomybatis.Controller"))
                        .paths(PathSelectors.any())
                        .build()
                        .apiInfo(apiInfo());
    }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("测试接口平台API")
                    .description("cathy demo  API.")
                            .termsOfServiceUrl("Terms of service")
                            .contact(new Contact("ylw","",""))
                            .version("1.0")
                            .build();
        }
    }

    在上面这个代码中,为swagger加入的部分是

    最后,访问http://localhost:8080/swagger-ui.html

  • 相关阅读:
    codeforces 552 C Vanya and Scales
    CGlib小记
    关于camera senor的power引脚问题
    Android实战简易教程-第四十五枪(SlideSwitch-好看又有用的开关button)
    iOS UITableViewCell 几个方法的优先级
    android.app.Fragment与android.support.v4.app.Fragment不一致

    Delphi ADOQuery连接数据库的查询、插入、删除、修改
    updatebatch
    类方法调用
  • 原文地址:https://www.cnblogs.com/lwyang/p/9808874.html
Copyright © 2011-2022 走看看