springboot 整合swagger 入门 使用

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

1.前提

一定要看好版本。

Springboot ✚ Swagger各版本整理_swagger版本_qq_33334411的博客-CSDN博客

我的版本:

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

2. 使用

新建一个boot web项目之后,导入上述依赖。

在confi包下新建一个SwaggerConfig.java配置类

Swgger2Config.java


@Configuration
@EnableSwagger2 // 3.0版本加不加无所谓
public class Swagger2Config {

    @Bean
    public Docket coreApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(adminApiInfo())
                .groupName("group1")
                .select()
                .build();
    }

    private ApiInfo adminApiInfo(){
        return new ApiInfoBuilder()
                .title("后台管理系统--api文档")
                .description("后台管理系统接口描述")
                .version("1.0")
                .contact(new Contact("郡主喵","http://baidu.com","728831102@qq.com"))
                .build();
    }
}

在controller包新建HelloController.java

@RestController
@ResponseBody
@Api(tags = "你好相关接口:")
public class HelloController {

    @GetMapping("/hellow")
    @ApiOperation("这是一个测试接口")
    public HelloVO hello(){
        return new HelloVO("qhx",11);
    }
}

在modle.vo下新建HelloVO.java

@ApiModel(value = "HelloVO",description = "你好相关接口的信息封装")
public class HelloVO {
    @ApiModelProperty("姓名")
    private String name;
    @ApiModelProperty("年龄")
    private Integer age;

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public HelloVO(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}

application.yml/properties文件中添加

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

运行,访问 http://localhost:8081/swagger-ui/index.html

springboot 整合swagger 入门 使用,工具类,spring boot,java,spring

 看到如上,及成功。

一般常用的swagger2注解

@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiModelProperty:用对象接收参数时,描述对象的一个字段
@ApiImplicitParam:一个请求参数 @ApiImplicitParams:多个请求参数

3.报错解决

1.Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

忘加这个。

springboot 整合swagger 入门 使用,工具类,spring boot,java,spring

 2. Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

springboot 整合swagger 入门 使用,工具类,spring boot,java,spring

SpringBoot2.6.x使用PathPatternMatcher匹配路径,Swagger引用的Springfox基于AntPathMatcher匹配路径。匹配方式不同,导致错误。

因此在application.yaml/properties 文件新加。


spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

3.访问页面为空

你项目中加了拦截器,需要在拦截器相应的放行静态资源。

当然,还有过滤器,过滤器的话,需要匹配路径,那么携带swagger和v2可以直接放行。

springboot 整合swagger 入门 使用,工具类,spring boot,java,spring

springboot集成swagger页面空白解决方法_swagger 空白_立码收复恶眛里恳的博客-CSDN博客文章来源地址https://www.toymoban.com/news/detail-656518.html

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

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

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

相关文章

  • 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)
  • 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)
  • 接口测试工具——Postman测试工具 & Swagger接口测试+SpringBoot整合 & JMeter高并发测试工具

    头部携带token 类型选择JSON https://apifox.com/ swagger2,较多 swagger3, Knife4j的前身是swagger-bootstrap=ui,前身swagger–bootstrap-ui是一个纯swagger–ui的ui皮肤项目 https://doc.xiaominfo.com/docs/quick-start 底层就是swagger 序号 注解 作用 1 @Api(tags = “图书的api接口类”) 左侧名字 2 @ApiOperation(“findPa

    2024年02月12日
    浏览(46)
  • spring boot整合第三方微信开发工具 weixin-java-miniapp 实现小程序微信登录

    有时候项目需要用到微信登录或获取用户的手机号码,weixin-java-miniapp是一个好用的第三方工具,不用我们自己写httpcline调用。 导入jar包 添加一个resource.properties文件,写上小程序的appid和secret 添加两个配置文件 WxMaProperties.java WxMaConfiguration.java 如何使用 小程序给微信发送消息

    2024年02月16日
    浏览(33)
  • 【Spring Boot】SpringBoot 2.6.6 集成 SpringDoc 1.6.9 生成swagger接口文档

    之前常用的SpringFox在2020年停止更新了,新项目集成SpringFox出来一堆问题,所以打算使用更活跃的SpringDoc,这里简单介绍一下我这边SpringBoot2.6.6集成SpringDoc1.6.9的demo。 官网链接 maven为例: 代码如下(示例): 默认路径: UI界面 http://localhost:9527/swagger-ui/index.html json界面 http:/

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

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

    2024年02月11日
    浏览(30)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包