Springboot集成Swagger及使用

 我们的口号是:十年生死两茫茫,写代码,到天亮!!!

Swagger官网:API Documentation & Design Tools for Teams | Swagger

第一步: 

创建一个Springboot项目导入依赖:

        
            io.springfox
            springfox-boot-starter
            3.0.0
        

第二步:

写一个hello工程

@RestController
public class HelloContorller {

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

第三步:

 启动后输入url访问地址:http://localhost:8080/swagger-ui/index.html

Springboot集成Swagger及使用_第1张图片

如果启动报此错:Failed to start bean 'documentationPluginsBootstrapper

那么就在配置文件添加:        

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

 Swagger信息设置

Swagger的bean实例Docket

package com.example.swaggertest.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;

@Configuration
@EnableSwagger2//开启Swagger
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());

    }

    public ApiInfo apiInfo(){
        return new ApiInfo(
                "喜羊羊的文档",//头部
                "十年生死两茫茫,写代码,到天亮",//描述
                "sb1.0",//版本
                "urn:tos",
                new Contact("喜羊羊","https:www.baidu.com/","[email protected]"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }
}

Springboot集成Swagger及使用_第2张图片

Swagger接口设置

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(false)//是否启用swagger,false为不能再浏览器中访问
                .select()
                //RequestHandlerSelectors.basePackage("") 扫描指定的包
                //RequestHandlerSelectors.any() 扫描全部
                //RequestHandlerSelectors.none() 不扫描
                //RequestHandlerSelectors.withClassAnnotation(RestController.class) 扫描类上此注解的类,参数是一个注解的反射对象
                //RequestHandlerSelectors.withMethodAnnotation(GetMapping.class) 扫描方法上有此注解的方法
                .apis(RequestHandlerSelectors.basePackage("com.example.controller"))
                //.paths(PathSelectors.ant("/user/**")) //过滤只请求带有user下的请求路径
                .build();
        
    }

Springboot集成Swagger及使用_第3张图片 

如何将Swagger在生产环境中使用,在发布的时候不使用?

1、判断是不是生产环境 。2、注入enable()

    @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())
                .enable(flag)//是否启用swagger,false为不能再浏览器中访问
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(RestController.class))
                .paths(PathSelectors.ant("/user/**")) //过滤只请求带有user下的请求路径
                .build();
    }

访问时要记得更改url中所用的环境的端口号进行访问。

Swagger组设置

@Configuration
@EnableSwagger2//开启Swagger2
public class SwaggerConfig {
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }
    @Bean
    public Docket docket4(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }

    @Bean
    public Docket docket(Environment environment){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("喜羊羊")
                .enable(true)//是否启用swagger,false为不能再浏览器中访问
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.swaggertest.controller"))
                //.paths(PathSelectors.ant("/user/**")) //过滤只请求带有user下的请求路径
                .build();
    }

    //配置Swagger信息=apiInfo
    public ApiInfo apiInfo(){
        return new ApiInfo(
                "喜羊羊的文档",
                "十年生死两茫茫,写代码,到天亮",
                "sb1.0",
                "urn:tos",
                //作者信息
                new Contact("喜羊羊","https:www.baidu.com/","[email protected]"),
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }
}

Springboot集成Swagger及使用_第4张图片

Swagger实体类信息设置

添加一个实体类

public class User {
    public int ID;
    public String Name;
    private int age;
}

添加一个接口

@RestController
public class HelloContorller {

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

    //只要接口中返回值存在实体类,就会被扫描到Swagger中
    @PostMapping("user/hello")
    public User hello2(){
        return new User();
    }
}

Springboot集成Swagger及使用_第5张图片

 Swagger注解使用

 Controller中的注解:有很多,列出简单几个的使用

@RestController
@Api(tags = "hello控制层")
public class HelloContorller {

    @GetMapping("hello")
    public String hello1(){
        return "hello";
    }


    @ApiOperation("user的hello")
    @PostMapping("user/hello")
    public User hello2(@ApiParam("用户名") String name){
        return new User();
    }
}

Springboot集成Swagger及使用_第6张图片

Springboot集成Swagger及使用_第7张图片

 实体类中的注解:

@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("用户id")
    public int ID;
    @ApiModelProperty("用户名")
    public String Name;
    @ApiModelProperty("年龄")
    private int age;
}

Springboot集成Swagger及使用_第8张图片

 Swagger扩展测试

@RestController
@Api(tags = "hello控制层")
public class HelloContorller {

    @GetMapping("hello")
    public String hello1(){
        return "hello";
    }


    @ApiOperation("user的hello")
    @PostMapping("user/hello")
    public String hello2(@ApiParam("用户") User user){
        String a = "成功";
        return a;
    }
}

Springboot集成Swagger及使用_第9张图片

Springboot集成Swagger及使用_第10张图片

Springboot集成Swagger及使用_第11张图片

 ok!!我们的口号是???​​​​​​​

你可能感兴趣的:(spring,json,spring,boot)