springboot整合swagger框架

yvioo      2022-05-03     109

关键词:

 

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>

 

 

创建配置类

SwaggerConfig.java
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;

@Api(description = "该控制器描述") @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整合swagger2框架

一:什么是SwaggerSwagger是一款通过我们添加的注解来对方法进行说明,来自动生成项目的在线api接口文档的web服务。二:添加Swagger2依赖<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><ve 查看详情

swagger和springboot整合

Swagger号称全世界最流行的api框架;RestFulApi文档在线自动生成工具=>Api文档与API定义同步更新配置<!--https://mvnrepository.com/artifact/io.springfox/springfox-swagger2--><dependency> <groupId>io.springfox</g 查看详情

springboot整合swagger-ui快速生成在线api文档

目录SpringBoot整合Swagger-ui实现在线API文档本篇要点一、restful风格简单介绍二、SpringBoot与Swagger-ui快速整合1、第一种方式:使用官方依赖2、第二种方式:使用第三方依赖三、swagger-ui的基本注解源码下载参考阅读SpringBoot整合Swagger-u... 查看详情

springboot+jpa+mysql+redis+swagger整合步骤

springboot+jpa+MySQL+swagger框架搭建好之上再整合redis:在电脑上先安装redis:一、在pom.xml中引入redis二、在application.yml里配置redis,单独说明:redis刚一开始安装好是没有设置密码的。否则,会报connection错误。三、在service配置Redis,... 查看详情

springboot整合springfox-swagger2

前言不管SpringBoot整合还是SpringMVC整合Swagger都基本类似,重点就在于配置Swagger,它的精髓所在就在于配置。@目录1、Swagger简介2、整合前可能遇到的问题3、SpringBoot集成Swagger4、配置Swagger4.1、Swagger四部分布局4.2、第二部分:API基... 查看详情

springboot整合swagger

本文github位置:https://github.com/WillVi/springboot-swagger2-demo环境准备JDK版本:1.8Springboot版本:1.5.16Swagger及其Swagger-ui版本:2.6.1(关于swagger-ui版本每个版本的汉化方式可能有不同)默认url:http://localhost:8080/swagger-ui.htmlMaven依赖 查看详情

springboot整合swagger2+knife4j

参考技术A前言:前面文件已经发过swagger2的整合教程SpringBoot整合swagger,本文主要是介绍knife4j。Knife4j是为JavaMVC框架集成Swagger生成Api文档的增强解决方案,前身是swagger-bootstrap-ui,取名kni4j是希望她能像一把匕首一样小巧,轻量,并且功... 查看详情

转:springboot与swagger整合出现unabletoinferbaseurl.thisiscommonwhenusingdynamic的解决办法

...链接:https://blog.csdn.net/miachen520/article/details/95718639今天在springboot与swagger整合测试的时候跳出如下所示界面 经查资料发现有两种解决办法,1.直接把@EnableSwagger2注解加在主启动类就可以,这样虽然能解决问题,但是这样会扫... 查看详情

springboot学习——springboot快速整合swagger文档

@[toc]简介优点后端根据swagger语法,自动生成漂亮规范的接口文档。做交互测试。劣势侵入式的,影响程序运行,尤其是传参的时候。注意swagger分1.2版本和2.0版本,差异较大。swagger1.2即swagger-ui;swagger2.0即springfox-swagger。本文介... 查看详情

swaggerlearing-springboot整合swagger

学习了一下swagger。这是编写的Demo源码https://github.com/AmberBar/Learning/tree/master/swagger-learning/swagger需要的小伙伴可以clone直接运行访问地址http://localhost:9999/swagger-ui.html#/  查看详情

在springboot中整合jersey和springfox-swagger2

前言为了实现RESTfulWebservice和接口说明,基于springboot平台,使用jersey作为JAX-RS的实现,采用swagger文档在线生成工具。提要在实现了springboot集成jersey的swagger文档功能,同时满足SpringMVC整合swagger提供RESTful文档功能。Springboot 集... 查看详情

springboot和swagger整合

 本文来源:https://blog.csdn.net/saytime/article/details/74937664一、依赖<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version& 查看详情

springboot整合swagger-ui

SpringBoot整合Swagger-ui引入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency>< 查看详情

springboot整合swagger2

一关于SwaggerSwagger能成为最受欢迎的RESTAPIs文档生成工具之一,有以下几个原因:Swagger可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API。Swagger可以生成客户端SDK代码用于各种不同的平台上的实现。Swagger... 查看详情

springboot之整合swagger2

参考技术A随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染、先后端分离的形态,而且前端技术和后端技术在各自的道路上越走越远。前端和后端的唯一联系,变成了API接口;API文档变成了... 查看详情

springboot整合swagger

...Api到Postman测试,耗费了不少时间。这次新项目决定使用SpringBoot来做,各方面都节省了不少配置,一想到Api的对接就有点头大,于是决定把Swagger集成进来,实现Api文档的自动生成(通过http://localhost:8080/swagge 查看详情

springboot整合swagger2

  手写Api文档的几个痛点:文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时。接口返回结果不明确不能直接在线测试接口,通常需要使用工具,比如postman接口文档太多,不好管理  Swagger也就是... 查看详情

springboot整合swagger测试api构建

@Author:SimpleWu什么是Swagger?Swagger是什么:THEWORLD’SMOSTPOPULARAPITOOLING根据官网的介绍:SwaggerInspector:测试API和生成OpenAPI的开发工具。SwaggerInspector的建立是为了解决开发者的三个主要目标。执行简单的API测试生成OpenAPI文档探索新... 查看详情