首页 > 其他分享 >spring boot集成swagger3

spring boot集成swagger3

时间:2023-03-16 13:33:52浏览次数:48  
标签:java spring boot springframework swagger3 context org

spring boot集成swagger3

 

spring boot集成swagger3

swagger2的整合:https://www.cnblogs.com/chenglc/p/10910721.html

swagger3的使用步骤和2略有差异

maven依赖

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

swagger3.0的依赖将springfox-swagger-uispringfox-swagger2整合到了springfox-boot-starter,还包括一些spring的依赖,说一不需要再去关心其他的依赖。

开启swagger

和2版本一样,需要一个swagger的配置,用于配置基本信息

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;

/**
 * swagger配置
 */
@Configuration
public class SwaggerConfig {

    Boolean swaggerEnabled = true;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                // 是否开启
                .enable(swaggerEnabled)//true
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.clc.resource"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("资源中心swagger业务")
                //创建人
                .contact(new Contact("clc", "http://www.baidu.com", "[email protected]"))
                .version("1.0")
                .description("不需要描述")
                .build();
    }
}

默认访问地址

http://localhost:port/swagger-ui/index.html

和2版本地址不同:http://localhost:port/swagger-ui.html

spring boot 2.6.x 版本配置

如果是spring boot 2.6.x集成了swagger,会出现异常信息:
Failed to start bean 'documentationPluginsBootstrapper';

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.14.jar:5.3.14]
	at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.14.jar:5.3.14]
	at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.14.jar:5.3.14]
	at org.springframework.context.support.DefaultLifecycleProcessor$$Lambda$627/1737023457.accept(Unknown Source) ~[na:na]
	at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_31]
	at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.14.jar:5.3.14]
	at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.14.jar:5.3.14]
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.14.jar:5.3.14]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.14.jar:5.3.14]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.2.jar:2.6.2]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) [spring-boot-2.6.2.jar:2.6.2]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) [spring-boot-2.6.2.jar:2.6.2]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) [spring-boot-2.6.2.jar:2.6.2]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) [spring-boot-2.6.2.jar:2.6.2]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290) [spring-boot-2.6.2.jar:2.6.2]

因为Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher

解决方案

加spring配置指定AntPathMatcher

spring.mvc.pathmatch.matching-strategy: ANT_PATH_MATCHER

使用示例

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * ClassName: JSR303Controller
 * Description:JSR303协议测试
 *
 * @author chengluchao
 * @date: 2022/1/11 14:49
 * @since 1.3.9
 */
@Api("JSR303协议")
@Validated
@RestController
@RequestMapping("/jsr303")
@Slf4j
public class JSR303Controller {

    @ApiOperation("批量保存数据")
    @ResponseBody
    @PostMapping("batchSave")
    public Object batchSave(@RequestBody @Validated(ValidationGroupSequence.class) BatchSysGroupDto dto) {
        return "Result.okInstance();";
    }
}

效果

 

 

swagger3.0新功能

  1. Webflux支持
  2. Spring Integration支持
  3. SpringBoot支持springfox Boot starter依赖性(零配置、自动配置支持)
  4. 支持OpenApi 3.0.3

CLC

标签:java,spring,boot,springframework,swagger3,context,org
From: https://www.cnblogs.com/manmanblogs/p/17222215.html

相关文章

  • SpringBoot相关问题
    参考:https://juejin.cn/post/6844904125709156359为什么需要spring-boot-maven-pluginspring-boot-maven-plugin提供了像jar一样打包或者运行应用程序的命令spring-boot......
  • springboot中配置elasticSearch
    1.1.1 在build.gradle中添加需要的jar包   我创建的gradle工程,对应的maven工程也是一样,添加对应的jar包即可//添加SpringDataElasticsearch的依赖comp......
  • spring boot jpa 数据库字段加密存储
     在SpringBootJPA中,可以通过自定义AttributeConverter类来实现数据库字段的加密存储。AttributeConverter是JPA2.1中引入的一个接口,用于在实体属性和数据库列......
  • SpringSecurity授权原理
    在第二部分中我们讲解的都是用户认证,不管是用户名密码,还是图形验证码等,最终的目的都是一个:让系统知道你到底是谁在访问你的系统,解决的问题是,你是谁?这部分主要讲......
  • Spring Session库
    SpringSecurity可以与SpringSession库配合使用,只需要做一些简单的配置就可以实现一些功能,如(会话过期、一个账号只能同时在线一个、集群session等)。1)配置session会话超......
  • 前后端分离项目(vue+springboot)集成pageoffice实现在线编辑office文件
    前后端分离项目下使用PageOffice原理图集成步骤前端vue项目在您Vue项目的根目录下index.html中引用后端项目根目录下pageoffice.js文件。例如:<scripttype="te......
  • cannot access class org.springframework.cglib.core.ReflectUtils with java 17
    (inmodulecom.xxx)cannotaccessclassorg.springframework.cglib.core.ReflectUtils(inunnamedmodule@0x2d950574)becausemodulecom.xxxdoesnotreadunname......
  • SpringCloud Alibaba Nacos 简介
    Nocos(DynamicNamingandConfigurationService)一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。可以说Nacos是注册中心和配置中心的组合。Nacos官网;h......
  • 【开源免费】使用Spring Boot和Html实现ChatGPT,1:亿还原,将就看
    highlight:a11y-dark简介前段时间写了一个Chatgpt的Java版SDK开源地址:chatgpt-java欢迎使用。但由于原来OpenAI并没有支持官网的chatgpt模型,所以使用起来相对没有官网......
  • SpringCloud Alibaba 入门简介
    为什么会出现SpringCloudAlibaba?因为SpringCloudNetflix项目进入维护模式。维护模式:意味着SpringCloud团队将不会再向模块添加新功能。将修复block级别的bug以......