Swagger


1.Swagger简介

说白了就是实时更新api接口,以免前后端工程师打架

2.新建一个springboot(Web)项目

2.1导入maven依赖

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

2.2新建一个conreoller

 

 

 

package com.yao.swagger.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}

2.2.1启动SpringbootSwaggerApplication主入口进入检查

(扩展:一个项目只写了一个controller那么这个项目有几个请求,答案为两个,因为无论是否有controller都至少有一个请求那就是error,就是那个报错页面)

2.3配置swagger基本信息

2.3.1创建一个config

 

 

 

package com.yao.swagger.config;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
}//以上含有的注解,是最简单一定要加的至少两个基本注解

 

2.3.2访问http://localhost:8080/swagger-ui.html测试,会发现直接就有了这个页面,我们明明都没有创建这个页面

 

 又由于swagger有一个bean对象在springboot中

我们接着写它的配置类:

package com.yao.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;

import java.util.ArrayList;

import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    //配置了Swapperbean实例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    //配置Swagger信息,就是一些默认的配置信息
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact(
                "幺幺",
                "https://www.cnblogs.com/yaoyaoo/",
                "892095368@qq.com");
        return new ApiInfo(
                "幺幺的SwaggerApi",
                "这个作者很强",
                "1.0",
                "https://www.cnblogs.com/yaoyaoo/",
                contact,
                //以下三个配置默认
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }
}

 

 

 

 

 

2.4配置扫描接口及开关

package com.yao.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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;

import java.util.ArrayList;
import java.util.Properties;

import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("A");
    }

    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("B");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("C");
    }


    //配置了Swapperbean实例
    @Bean
    public Docket docket(Environment environment){
        // 设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev","test");
        //  通过环境监听查看自己是否处在设置的环境之中
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("幺幺")
                //enable如果为false,则swagger就不能在浏览器中访问
                //enable的应用就主要在于,生产环境中使用swagger,发布时就不使用swagger,那么其实我们只用
                // 去改变这个enable的值就可以了
                .enable(flag)
                .select()
                //select和build是一套,apis和paths只能在select和build之间来配置
                //RequestHandlerSelectors  配置要扫描接口的方式
                //basePackage指定该扫描的包
                //RequestHandlerSelectors.any()扫描所有包
                //RequestHandlerSelectors.none()都不扫描
                //RequestHandlerSelectors.withClassAnnotation()扫描类上的注解需要传入一个反射出来的类
                //比如说传入一个xxx。class
                //RequestHandlerSelectors.withMethodAnnotation()扫描方法上的注解
                //因为有一些注解是用在类上的,有一些是放在方法上的比如@Controller这个注解就只能放在类上因
                // 此它只能放在RequestHandlerSelectors.withClassAnnotation()中
                //RequestHandlerSelectors.withClassAnnotation(RestController.class)就只会扫描有
                // RestController注解的类
                .apis(RequestHandlerSelectors.basePackage("com.yao.swagger.controller"))
                //paths代表可以过滤什么路径,只扫描带有yao下面请求的接口,打个比方RequestMapping("/hello")
                // 就不会被扫描,但是RequestMapping("/yao/hello")就可以被扫描
                //PathSelectors.any()就是什么都会扫描到,none()等等以此类推
                .paths(PathSelectors.ant("yao/**"))
                .build();
    }

    //配置Swagger信息,就是一些默认的配置信息
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact(
                "幺幺",
                "https://www.cnblogs.com/yaoyaoo/",
                "892095368@qq.com");
        return new ApiInfo(
                "幺幺的SwaggerApi",
                "这个作者很强",
                "1.0",
                "https://www.cnblogs.com/yaoyaoo/",
                contact,
                //以下三个配置默认
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }
}

  主要就是在docket里面去配置

 

 2.4.1在类上配置注解

package com.yao.swagger.pojo;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("用户实体类")
public class User {

    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;
}

  

 

 

 

 除了在实体类上还可以在controller中

 

 

 

 实时测试

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM