swagger 2.10.5 整合 spring boot

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

参考:
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.引入依赖


        <!-- (io.springfox):springfox-swagger2, springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.10.5</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.10.5</version>
        </dependency>
        <!-- 2.10.5 版本需要该依赖:springfox-spring-webmvc -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-spring-webmvc</artifactId>
            <version>2.10.5</version>
        </dependency>


2.配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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.EnableSwagger2WebMvc;


/**
 * 访问:http://localhost:8080/swagger-ui.html#/
 */
@Configuration
@EnableSwagger2WebMvc
public class Swagger2Config {
    @Value("${spring.profiles.active}")
    private String active;

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


    @Bean
    public Docket createRestApi() {

        return new Docket(DocumentationType.SWAGGER_2)
                .enable(!"prod".equals(active))
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
    }



    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Redis API 接口文档")
                .description("Redis REST API 接口文档")
                .termsOfServiceUrl("")
                .contact(new Contact("维护人xxx","维护人url","维护人email"))
                .license("xxx License Version 2.0")
                .licenseUrl("http://www.xxx.xxx/licenses/LICENSE-2.0")
                .version("1.0")
                .build();
    }

}


3.Controller层与入参实体类编写:

实体类:
@ApiModel
@NoArgsConstructor
@AllArgsConstructor
@Data
public class MapVo {

    @ApiModelProperty(value = "key")
    private String key;

    @ApiModelProperty(value = "value")
    private String value;
}


处理控制器:
@Api(value = "xxx接口", tags = "xxx 接口操作")
@Controller
@RequestMapping("/string")
public class TestConroller {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation("set 操作 - 实体类")
    @ResponseBody
    @RequestMapping(value = "/setvo", method = RequestMethod.POST)
    public String setValues(@RequestBody MapVo mapVo) {

        redisTemplate.opsForValue().set(mapVo.getKey(), mapVo.getValue());

        return "success";
    }

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

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

        return result;
    }

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

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

        return "success";
    }


    @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";
    }

}

4.访问:http://localhost:8080/swagger-ui.html#/

5.常用注解

@EnableSwagger2WebMvc
@Api
@ApiOperation
@ApiModel 和 @ApiModelProperty
@ApiParam
@ApiImplicitParams 和 @ApiImplicitParam
@ApiResponses 和 @ApiResponse


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

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

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

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

相关文章

  • Spring Boot整合swagger使用教程(这一篇就够了)

    你可能尝试过写完一个接口后,自己去创建接口文档,或者修改接口后修改接口文档。多了之后,你肯定会发生一个操作,那就是忘记了修改文档或者创建文档(除非你们公司把接口文档和写接口要求得很紧密😓忘记写文档就扣工资?,否则两个分离的工作总是有可能遗漏的

    2024年01月17日
    浏览(31)
  • 【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)
  • Spring Boot3整合knife4j(swagger3)

    目录 1.前置条件 2.导依赖 3.配置 已经初始化好一个spring boot项目且版本为3X,项目可正常启动。 作者版本为3.2.2 初始化教程: 新版idea创建spring boot项目-CSDN博客 https://blog.csdn.net/qq_62262918/article/details/135785412?spm=1001.2014.3001.5501 knife4j官网: Knife4j · 集Swagger2及OpenAPI3为一体的增强

    2024年01月23日
    浏览(34)
  • Spring Boot 2.6 以上整合 Swagger + Knife4j 报错

    这个问题主要出现在 Spring Boot 2.6 及以后,只要是 Spring Boot 2.6 引入的新 PathPatternParser 导致的。 两种解决办法 Path匹配策略切换回 ​​ant_path_matcher ​ 添加下面这个Bean的定义

    2024年01月17日
    浏览(42)
  • Spring Boot入门(16):Spring Boot 整合 Swagger-UI 实现在线API接口文档 | 超级详细,建议收藏

            在现代化的软件开发中,API接口文档的编写和管理是非常重要的一环。而Swagger-UI作为一款优秀的API文档生成工具,可以帮助开发者轻松地生成并管理API接口文档,提高开发效率和代码质量。在本文中,我们将介绍如何使用Spring Boot框架和Swagger-UI工具实现在线API接

    2024年02月16日
    浏览(42)
  • spring boot 集成 swagger3

              Swagger 3是一种开源的API描述工具,它可以帮助开发人员设计、构建、文档化和测试API。Swagger 3支持多种编程语言和框架,包括Java、Node.js、Python、Ruby等,并提供了许多集成工具和插件,例如Postman、Apigee等。 Swagger 3使用OpenAPI规范来描述API,这是一种通用的API描述

    2024年02月06日
    浏览(32)
  • Spring Boot 中如何使用 Swagger

    在开发 Web 应用时,API 文档的编写和维护是一项非常重要的工作。Swagger 是一款非常流行的 API 文档工具,可以自动生成 API 文档,并提供一系列的交互式工具,如测试界面、调试界面等,方便开发者进行 API 的调试和测试。本文将介绍如何在 Spring Boot 应用中使用 Swagger。 首先

    2024年02月11日
    浏览(29)
  • Spring Boot 禁用 Swagger 的三种方式

    禁用方法1: ====== 使用注解 @Value() 推荐使用 package com.dc.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; impo

    2024年04月22日
    浏览(32)
  • spring boot未授权访问及Swagger漏洞处理

    无需修改源码,处理spring boot未授权访问及Swagger漏洞处理 风险程度 :【高危】 漏洞概述 : 未授权访问可以理解为需要安全配置或权限认证的地址、授权页面存在缺陷,导致其他用户可以直接访问,从而引发重要权限可被操作、数据库、网站目录等敏感信息泄露。登陆验证一

    2024年02月16日
    浏览(28)
  • 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日
    浏览(46)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包