SpringBoot整合Swagger框架过程解析

所属分类: 软件编程 / java 阅读数: 48
收藏 0 赞 0 分享

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

总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法、参数和模型紧密集成到服务器端的代码,允许 API 来始终保持同步。Swagger 让部署管理和使用功能强大的 API 从未如此简单。

引入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>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.10</version>
      <scope>provided</scope>
    </dependency>

创建配置类

package com.example.demo.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.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 yvioo。
 */
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {


  /**
   * 配置Swagger的Docket的bean实例
   * @return
   */
  @Bean
  public Docket docket(Environment environment) {

    //设置只在开发中环境中启动swagger
    Profiles profiles=Profiles.of("dev");

    //表示如果现在是dev环境,则返回true 开启swagger
    boolean flag=environment.acceptsProfiles(profiles);



    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        //是否启动swagger 默认启动
        .enable(flag)
        //所在分组
        .groupName("yvioo")
        .select()
        //指定扫描的包路径
        .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
        //指定扫描的请求,这里表示扫描 /hello/ 的请求
        //.paths(PathSelectors.ant("/hello/**"))
        .build();
  }


  /**
   * 配置ApiInfo信息
   * @return
   */
  private ApiInfo apiInfo() {

    //作者信息
    Contact author = new Contact("yvioo", "https://www.cnblogs.com/pxblog/", "111@qq.com");


    return new ApiInfo(
        "Swagger测试",
        "Swagger描述",
        "1.0",
        "urn:tos",
        author,
        "Apache 2.0",
        "http://www.apache.org/licenses/LICENSE-2.0",
        new ArrayList()
    );

  }
}

测试用户实体类

User.java

package com.example.demo.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel("用户实体类 User") //增加实体类接口注释
@Data  //使用Lombok插件自动生成get set方法,这样才能在swagger中显示属性值
public class User {
  @ApiModelProperty("用户ID") //增加字段接口注释
  private Integer id;
  @ApiModelProperty("用户名")
  private String username;
}

测试控制器

SwaggerController.java

package com.example.demo.controller;


import com.example.demo.entity.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SwaggerController {


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


  /**
   * 接口返回值含有实体类,就会被swagger扫描
   *
   * @return
   */
  @ApiOperation("查询用户方法注释")
  @GetMapping(value = "/get/{id}")
  public User get(@ApiParam("请求参数注释") @PathVariable(value = "id")Integer id){
    return new User();
  }
}

使用dev环境 启动项目后 浏览器打开http://localhost:8081/swagger-ui.html#/ 我这里用的端口是8081

显示效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多