Swagger

suaxi
2020-12-26 / 1 评论 / 298 阅读 / 正在检测是否收录...

Swagger

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。

1、导入依赖

<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>

2、SwaggerConfig配置

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

/**
 * @Author suaxi
 * @Date 2020/12/26 11:03
 */

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    //配置Swagger的Docket
    @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,默认为true,如需关闭须手动设置为false
                .select()
                //RequestHandlerSelectors 配置扫描接口的方式
                //basePackage() 指定要扫描的包
                //any() 扫描全部
                //none() 不扫描
                //withClassAnnotation(Controller.class) 扫描类注解(参数是一个注解的反射对象)
                //withMethodAnnotation(RequestMapping.class) 扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.sw.swagger.controller"))
                //paths() 过滤的路径
                .paths(PathSelectors.ant("/**"))
                .build();
    }

    //配置Swagger apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("suaix","http://wangchouchou.com","281463547@qq.com");
        return new ApiInfo(
                "Swagger StudyDemo",
                "SwaggerStudy",
                "1.0",
                "http://wangchouchou.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}

Docket配置

//配置Swagger的Docket
    @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,默认为true,如需关闭须手动设置为false
                .select()
                //RequestHandlerSelectors 配置扫描接口的方式
                //basePackage() 指定要扫描的包
                //any() 扫描全部
                //none() 不扫描
                //withClassAnnotation(Controller.class) 扫描类注解(参数是一个注解的反射对象)
                //withMethodAnnotation(RequestMapping.class) 扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.sw.swagger.controller"))
                //paths() 过滤的路径
                .paths(PathSelectors.ant("/**"))
                .build();
    }

配置什么环境下需要使用Swagger

通过监听配置文件实现

//配置那种环境下需要启用Swagger
Profiles profiles = Profiles.of("dev", "test");
//监听配置文件
boolean flag = environment.acceptsProfiles(profiles);

3、配置文档分组

.groupName("Demo")

如何配置实现多个分组?

配置多个Docket实例即可

4、实体类

package com.sw.swagger.pojo;

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

/**
 * @Author suaxi
 * @Date 2020/12/26 14:39
 */

//@Api("用户实体类")
@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;
}

5、Controller

package com.sw.swagger.controller;

import com.sw.swagger.pojo.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author suaxi
 * @Date 2020/12/26 10:59
 */

@RestController
public class HelloController {

    @ApiOperation("POST测试")
    @PostMapping("/hello")
    public String helloTest(@ApiParam("用户名") String username){
        return "swagger-springboot";
    }

    @ApiOperation("返回User对象")
    @PostMapping("/u")
    public User user(){
        return new User();
    }

    @ApiOperation("Hello控制类")
    @GetMapping("/h")
    public String hello(){
        return "swagger-springboot";
    }
}

小结:

1、通过Swagger可以给一些难以理解的属性或接口添加注释信息

2、接口文档实时更新

3、在线测试

0

评论 (1)

取消
  1. 头像
    Tracee Siggers
    Windows 10 · Google Chrome

    Let me just say your site is amazing! It is well put together and easy to navigate which is a plus. With such a nice layout you must attract a lot of visitors. I just wanted to give you a heads up because your site inspired me to build my own. I hope everything is going great and much success in your future. Thank and have the best of day!

    回复