swagger3 快速整合 springboot 2.6.15

这篇具有很好参考价值的文章主要介绍了swagger3 快速整合 springboot 2.6.15。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

参考:
https://swagger.io/
https://swagger.io/docs/
https://swagger.io/tools/swagger-ui/
https://github.com/swagger-api/swagger-core/wiki/
https://github.com/swagger-api/swagger-ui/wiki

http://springfox.github.io/springfox/
https://github.com/springfox/springfox
http://springfox.github.io/springfox/docs/current/
https://github.com/springfox/springfox-demos
https://github.com/springfox/springfox-demos/tree/2.9.2
https://github.com/springfox/springfox-demos/tree/3.0.0


1.引入依赖

    <!-- springfox-boot-starter -->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-boot-starter</artifactId>
      <version>3.0.0</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.6.15</version>
    </dependency>


2.配置类

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 访问:http://localhost:8080/swagger-ui/
 */
@Configuration
@EnableOpenApi
public class Swagger3Config {


    private String basePackage = "com.xxx.xxx.controller";


    @Bean
    public Docket createRestApi() {

        return new Docket(DocumentationType.OAS_30)
                .groupName("api")
                .enable(true) // .enable(!"prod".equals(active))
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage)) // 基于包扫描
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))  // 基于注解
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) // 基于注解
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API 接口文档")
                .description("Restful API 接口文档")
                .version("1.0")
                .contact(new Contact("联系人姓名","联系人url","联系人email"))
                .termsOfServiceUrl("服务条款URL")
                .license("xxx License Version 1.0")
                .licenseUrl("http://www.xxx.xxx/licenses/LICENSE-1.0")
                .build();
    }




    /**
     * 增加如下配置可解决Spring Boot 6.x 与Swagger 3.0.0 不兼容问题
     **/
    @Bean
    public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,
                                                                         ServletEndpointsSupplier servletEndpointsSupplier,
                                                                         ControllerEndpointsSupplier controllerEndpointsSupplier,
                                                                         EndpointMediaTypes endpointMediaTypes,
                                                                         CorsEndpointProperties corsProperties,
                                                                         WebEndpointProperties webEndpointProperties,
                                                                         Environment environment) {
        List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
        Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
        String basePath = webEndpointProperties.getBasePath();
        EndpointMapping endpointMapping = new EndpointMapping(basePath);
        boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
    }


    private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
        return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
    }

}



3.编写代码Controller

@Api(tags = "测试接口")
@Controller
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RedisTemplate redisTemplate;


    @ApiOperation("set value 操作")
    @ResponseBody
    @RequestMapping(value = "/set", method = RequestMethod.POST)
    public String setVal(String key, String value) {

        redisTemplate.opsForValue().set(key, value);

        return "success set val";
    }

    @ApiOperation("get 操作")
    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public String getValue(String key) {

        String result = (String) redisTemplate.opsForValue().get(key);

        System.err.println("======> 返回结果result:" + result);

        return result;
    }


}


4.访问与测试:http://localhost:8080/swagger-ui/



文章来源地址https://www.toymoban.com/news/detail-681330.html

到了这里,关于swagger3 快速整合 springboot 2.6.15的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringBoot3整合OpenAPI3(Swagger3)

    swagger2 更新到3后,再使用方法上发生了很大的变化,名称也变为 OpenAPI3 。 官方文档 openapi3 使用十分方便,做到这里后,你可以直接通过以下网址访问 swagger 页面。 1. @OpenAPIDefinition + @Info 用于定义整个 API 的信息,通常放在主应用类上。可以包括 API 的标题、描述、版本等信

    2024年01月22日
    浏览(45)
  • Spring Boot 3项目集成Swagger3教程

    欢迎来到我的小天地,这里是我记录技术点滴、分享学习心得的地方。📚 🛠️ 技能清单 编程语言 :Java、C、C++、Python、Go、 前端技术 :Jquery、Vue.js、React、uni-app、Echarts UI设计 : Element-ui、Antd、Color-ui 后端技术 :Spring Boot、Mybatis-plus、Swagger 移动开发 :Android 操作系统 :

    2024年04月17日
    浏览(47)
  • 【Spring Boot】SpringBoot 优雅整合Swagger Api 自动生成文档

    Swagger 是一套 RESTful API 文档生成工具,可以方便地生成 API 文档并提供 API 调试页面。 而 Spring Boot 是一款非常优秀的 Java Web 开发框架,它可以非常方便地构建 Web 应用程序。 在本文中,我们将介绍如何使用 Swagger 以及如何在 Spring Boot 中整合 Swagger 。 首先,在 pom.xml 文件中添

    2023年04月22日
    浏览(34)
  • SpringCloudGateway整合swagger3文档

             SpringCloud项目中,微服务模块和网关模块必不可少。按照以前SpringBoot的模式,单个服务拥有自己的Api文档(Swagger文档),引入微服务后,多文档管理成了一个问题。我们需要一个统一的入口方便前端同学查看。本篇文章就是把各个微服务的swagger-api文档,集成到网

    2024年02月09日
    浏览(33)
  • Spring Boot 整合 Swagger 教程详解

    ✅作者简介:2022年 博客新星 第八 。热爱国学的Java后端开发者,修心和技术同步精进。 🍎个人主页:Java Fans的博客 🍊个人信条:不迁怒,不贰过。小知识,大智慧。 💞当前专栏:SpringBoot 框架从入门到精通 ✨特色专栏:国学周更-心性养成之路 🥭本文内容:Spring Boot 整

    2023年04月14日
    浏览(31)
  • Java技术-接口文档-Swagger2&Swagger3&接口文档UI整合

    目录 一、Swagger2完整用法 1.POM依赖 2.接口类 3.实现类 4.托管静态资源 5.接口文档配置 6.生产环境关闭接口文档 7.Swagger3页面效果 二、Swagger3完整用法 三、Swagger整合Knife4jUi 1.POM依赖 2.接口类 3.实现类 4.托管静态资源 5.接口文档配置 6.生产环境关闭接口文档 四、注释和参数讲解

    2024年02月16日
    浏览(36)
  • swagger 2.10.5 整合 spring boot

    2024年02月11日
    浏览(31)
  • Spring Boot 整合 Swagger2 纠错

            因为我要建立的是微服务的项目,需要建立许多模块,以至于我在父工程中引入了当前模块,然后我在子模块中又引入了当前模块,造成了冲突。         另外一种解决方法是,经过上网查证,可能由于Spring Boot和Swagger版本的问题,Spring Boot2.6以上的版本,需要使用

    2024年02月12日
    浏览(26)
  • Spring Boot整合Spring Fox生成Swagger文档

    Springfox是一个用于在Spring应用程序中生成Swagger文档的开源库。它提供了一组注解和工具,可以将你的API代码和文档整合在一起,方便生成和展示API的Swagger文档。 使用Springfox,你可以在Spring Boot项目中集成Swagger,并通过Swagger UI查看和测试API。它提供了一些注解,如 @Api 、 @

    2024年02月08日
    浏览(32)
  • spring boot 2.7.9 整合 Swagger 3.0

     jdk  1.8 springboot 2.7.9 swagger 3.0.0 描述:Failed to start bean \\\'documentationPluginsBootstrapper\\\'; nested exception is java.lang.NullPointerException 没有这个bean,空指针了。 据网上资料找,3.0的Swagger已经不继承WebMvcConfig这个类,是继承了WebMvcConfigSupport类,从而改动了配置路径规则,然后报空指针,

    2024年02月06日
    浏览(65)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包