首页 > 基础资料 博客日记

SpringBoot3.1.2 引入Swagger报错Type javax.servlet.http.HttpServletRequest not present解决办法

2023-12-18 21:28:59基础资料围观356

文章SpringBoot3.1.2 引入Swagger报错Type javax.servlet.http.HttpServletRequest not present解决办法分享给大家,欢迎收藏Java资料网,专注分享技术知识

1.问题描述:

引入swagger2.X版本后,导致SpringBoot3.1.2启动报错

2.原因分析:

SpringBoot3.1.2版本依赖于jakarta依赖包,但是Swagger依赖底层应用的javax依赖包,所以只要一启动就会报错。

3.解决办法:

pom.xml文件,移除swagger2

<dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>2.9.2</version>
 </dependency>

 <dependency>
     <groupId>com.github.xiaoymin</groupId>
     <artifactId>swagger-bootstrap-ui</artifactId>
     <version>1.9.0</version>
     <scope>provided</scope>
 </dependency>

加入

<dependency>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-annotations</artifactId>
     <version>2.2.15</version>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.1.0</version>
</dependency>

代码修改

@Api(tags = "")     
改为     
@Tag(name = "")

@ApiModel(value="", description="")     
改为    
@Schema(name="", description="")

@ApiModelProperty(value = "", required = true)     
改为    
@Schema(name= "", description = "", required = true)

@ApiOperation(value = "", notes = "")    
改为    
@Operation(summary = "", description = "")

@ApiParam    改为     @Parameter

@ApiResponse(code = 404, message = "")  
改为
@ApiResponse(responseCode = "404", description = "")

SwaggerConfig.java

package com.example.lyc.springboot.demo.config;

import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SwaggerConfig {

    @Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group("public")
                .pathsToMatch("/public/**")
                .build();
    }

    @Bean
    public GroupedOpenApi privateApi() {
        return GroupedOpenApi.builder()
                .group("private")
                .pathsToMatch("/private/**")
                .build();
    }
}

实体类

package com.example.lyc.springboot.demo.entity;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@Schema(name = "User", description = "用户实体类")
public class User {
    @Schema(name = "id", description = "用户id")
    private int id;
    @Schema(name = "userName", description = "用户名")
    private String userName;
    @Schema(name = "password", description = "密码")
    private String password;

    /* 省略get、set和带参构造方法 */

}

controller类

package com.example.lyc.springboot.demo.controller;

import com.example.lyc.springboot.demo.config.MicroServiceUrl;
import com.example.lyc.springboot.demo.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Value;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/v1")
@Schema(name="", description="")
@Tag(name = "JsonController tags")
public class JsonController {
    @Value("${microservice.url.orderUrl}")
    String orderUrl;// orderUrl变量值 就是application.yml的url.orderUrl值
    @Autowired
    private User user;// 注入
    @Autowired
    MicroServiceUrl MicroServiceUrl; // 注入配置类

    @GetMapping("/user")
    @ResponseBody
    @Operation(summary = "获取用户信息", description = "获取用户信息")
    public User getUser() {
        user.setId(1);
        user.setUserName("李银池");
        user.setPassword("123456");
        // 日志输出
        log.info("user:{}", user);
        // 返回
        return user;
    }

    /***
     * @Description: list
     * @Author: liyinchi
     * @Date: 2023/9/18 11:17
     * @return map
     */
    @GetMapping("/list")
    @ResponseBody
    @Operation(summary = "获取用户列表", description = "获取用户列表")
    public List<User> getUserList() {
        List<User> userList = new ArrayList<>();
        User user1 = new User(1, "李银池", "123456");
        User user2 = new User(2, "王哈哈", "123456");
        userList.add(user1);
        userList.add(user2);
        // 日志输出
        log.info("userList:{}", userList);
        // 返回数据
        return userList;
    }

    /***
     * @Description: map
     * @Author: liyinchi
     * @Date: 2023/9/18 11:17
     * @return map
     */
    @GetMapping("/map")
    @ResponseBody
    @Operation(summary = "获取用户信息", description = "获取用户信息")
    public Map<String, Object> getMap() {
        Map<String, Object> map = new HashMap<>(3);
        User user = new User(1, "李银池", "123456");
        map.put("作者信息", user);
        map.put("github", "https://github.com/liyinchigithub");
        map.put("CSDN地址", "https://blog.csdn.net/u013302168");
        map.put("粉丝数量", 252);
        // 日志输出
        log.info("map:{}", map);
        // 返回
        return map;
    }

    /***
     * @Description: 读取application.yml配置文件参数
     * @Author: liyinchi
     * @Date: 2023/9/18 11:17
     * @return map
     */
    @GetMapping("/getYMLConfig")
    @ResponseBody
    @Operation(summary = "获取配置文件参数", description = "获取配置文件参数")
    public Map<String,Object> getYMLConfig() {
        Map<String,Object> orderUrlList = new HashMap();
        orderUrlList.put("microservice url orderUrl",orderUrl);
        log.info("orderUrl:{}", orderUrl);
        return orderUrlList;
    }

    /***
     * @Description: 读取application.yml配置文件参数
     * @Author: liyinchi
     * @Date: 2023/9/18 11:17
     * @return map
     */
    @GetMapping("/getYMLConfigClass")
    @ResponseBody
    @Operation(summary = "获取配置文件参数", description = "获取配置文件参数")
    public Map<String,Object> getYMLConfigClass() {
        Map<String,Object> orderUrlList = new HashMap();
        orderUrlList.put("orderUrl",MicroServiceUrl.getOrderUrl());
        orderUrlList.put("userUrl",MicroServiceUrl.getUserUrl());
        orderUrlList.put("shoppingUrl",MicroServiceUrl.getShoppingUrl());
        log.info("getYMLConfigClass orderUrlMap:{}", orderUrlList);
        return orderUrlList;
    }

    /***
     * @Description: 获取请求体
     * @Date: 20239/18 14:00
     * @param User user
     * @return map
     */
    @PostMapping("/addUser")
    @ResponseBody
    @Operation(summary = "获取请求体", description = "获取请求体")
    public Map<String,Object> getUser(@RequestBody @Parameter User user) { // 获取请求体
        Map<String,Object> map = new HashMap();
        map.put("user",user);
        return map;
    }

}

遇到报错:

java.lang.StringIndexOutOfBoundsException: begin 11, end 10, length 10

原因分析:

由于Springdoc OpenAPI UI的版本与Spring Boot的版本不兼容导致的。你正在使用的Springdoc OpenAPI UI的版本是2.1.0,而Spring Boot的版本是3.1.2。这两个版本可能存在不兼容的问题。

解决办法:

你可以尝试将Springdoc OpenAPI UI的版本升级到与Spring Boot 3.1.2更兼容的版本。你可以在Springdoc的GitHub仓库的release页面查看各个版本的发布说明,找到一个与Spring Boot 3.1.2兼容的版本。

然后,你可以在你的pom.xml文件中更新Springdoc OpenAPI UI的版本:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>新的版本号</version>
</dependency>

https://github.com/springdoc/springdoc-openapi/releases

 

 修改成支持springboot3.1.2的版本2.2.0后,成功~


文章来源:https://blog.csdn.net/u013302168/article/details/132993759
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云