springboot 升级6.7后swagger的适配

前情提要:

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.9</version> <!-- lookup parent from repository -->
</parent>
<!-- Swagger3依赖 -->
                    <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-boot-starter</artifactId>
                <version>${swagger.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>io.swagger</groupId>
                        <artifactId>swagger-models</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

报错信息:

Swagger-Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is “空指针异常“

解决方案:

1.降低Springboot版本 从2.6.9 降至2.5.7

 <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.5.8</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

 2.SwaggerConfig.java增加

import java.util.Collection;
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.core.env.Environment;
import org.springframework.util.StringUtils;
 /**
     * 增加如下配置可解决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));
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于springboot+ssm+swagger前后端分离的咨询管理系统源码(高分毕设).zip 基于spring
要整合SwaggerSpring Boot,需要进行以下步骤: 1. 在pom.xml文件中添加Swagger依赖: ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency> ``` 2. 创建Swagger配置类,用于配置Swagger的基本信息和扫描的包路径: ``` @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot REST API") .description("Spring Boot REST API for Online Store") .version("1.0.0") .build(); } } ``` 3. 在Controller类中添加Swagger注解,用于描述接口信息: ``` @RestController @RequestMapping("/api") @Api(value = "onlineStore", description = "Operations pertaining to products in Online Store") public class ProductController { @Autowired private ProductService productService; @ApiOperation(value = "View a list of available products", response = List.class) @GetMapping("/products") public List<Product> getAllProducts() { return productService.getAllProducts(); } @ApiOperation(value = "Get a product by Id") @GetMapping("/products/{id}") public Product getProductById(@PathVariable(value = "id") Long productId) { return productService.getProductById(productId); } @ApiOperation(value = "Add a product") @PostMapping("/products") public Product addProduct(@RequestBody Product product) { return productService.addProduct(product); } @ApiOperation(value = "Update a product") @PutMapping("/products/{id}") public Product updateProduct(@PathVariable(value = "id") Long productId, @RequestBody Product productDetails) { return productService.updateProduct(productId, productDetails); } @ApiOperation(value = "Delete a product") @DeleteMapping("/products/{id}") public ResponseEntity<?> deleteProduct(@PathVariable(value = "id") Long productId) { productService.deleteProduct(productId); return ResponseEntity.ok().build(); } } ``` 4. 启动Spring Boot应用程序,访问http://localhost:8080/swagger-ui.html,即可查看Swagger UI界面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值