Giter Club home page Giter Club logo

springboot-swagger2's Introduction

Swagger2简介

1.随项目自动生成强大RESTful API文档,减少工作量 2.API文档与代码整合在一起,便于同步更新API说明 3.页面测试功能来调试每个RESTful API

springboot集成Swagger2步骤

1. 新建一个Springboot项目

在这里插入图片描述

2. 导入依赖

在这里插入图片描述

		  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
		 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

3. 编写控制器

在这里插入图片描述

@RestController
public class Mycontroller {

    @GetMapping({"/","hello"})
    public String hello(){
        return "helloword";
    }
}

4. 编写swagger的配置类

写上注解 @Configuration @EnableSwagger2 在这里插入图片描述

@Configuration
@EnableSwagger2
public class SwaggerConfig {
}

启动项目运行 浏览器输入 http://localhost:8080/swagger-ui.html 在这里插入图片描述 我们所写的controller方法都会被检测到

5. 配置Swagger2

  1. 配置Swagger信息 info 在这里插入图片描述
    @Bean
    public Docket docket() {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apinfo());
    }
    public ApiInfo apinfo(){
        Contact contact = new Contact(
                "joker_dj",
                "https://www.cnblogs.com/joker-dj/",
                "[email protected]");
        return new ApiInfo("joker_dj的api文档",
                "Api Documentation",
                "1.0",
                "https://www.cnblogs.com/joker-dj/",
                contact, "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

运行看效果

在这里插入图片描述 2. 配置多个分组 多配置几个Docket即可 在这里插入图片描述 在这里插入图片描述 3. 配置其他的信息 不一一解释 都有注释 在这里插入图片描述

@Bean
    public Docket docket(){

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apinfo())
                //是否启用swagger false不启用
                .enable(true)
                .groupName("joker_dj")
                .select()
                /*
                配置RequestHandlerSelectors 扫描接口的方式
                指定扫描的包basePackage
                any()扫描全部
                none()不扫描
                withClassAnnotation:扫描类上的注解
                */
                .apis(RequestHandlerSelectors.basePackage("com.dj.controller"))
                //过滤路径 比如PathSelectors.ant("login/**")
               // .paths(PathSelectors.ant(""))
                .build();
    }

6.添加注释信息

  1. 编写user实体 在这里插入图片描述
  2. 在controller返回一个user 在这里插入图片描述 运行 在这里插入图片描述 会检测到我们所有写的controller方法 也可以进行测试方法 点击Try it out 在这里插入图片描述
  3. 点击运行 在这里插入图片描述 这里就会返回我们测试接口的信息以及响应的结果 在这里插入图片描述
  4. 给类,属性和方法添加注释信息 在user实体类添加注解 @ApiModel:给类添加注释 @ApiModelProperty :给字段添加注释 在这里插入图片描述
@ApiModel("用户实体类")
public class user {
    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;
}

运行看效果 在这里插入图片描述 添加到controller上 @ApiOperation 在这里插入图片描述 运行看效果 在这里插入图片描述 5. 发送数据 测试接口 在controller中编写代码 在这里插入图片描述 运行测试 在这里插入图片描述 输入用户名 密码 点击测试 在这里插入图片描述 在这里插入图片描述 这样就返回了我们数据

总结:Swagger这个工具主要是针对开发人员测试接口来使用的, 注意:在项目上线的时候己得吧Swagger给关闭 防止接口暴露 (安全)

springboot-swagger2's People

Contributors

2332810801 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.