1.问题描述:

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

2.原因分析:

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

3.解决办法:

pom.xml文件,移除swagger2

 io.springfox springfox-swagger2 2.9.2   com.github.xiaoymin swagger-bootstrap-ui 1.9.0 provided 

加入

io.swagger.core.v3swagger-annotations 2.2.15org.springdocspringdoc-openapi-starter-webmvc-ui2.1.0

代码修改

@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;@Configurationpublic class SwaggerConfig {@Beanpublic GroupedOpenApi publicApi() {return GroupedOpenApi.builder().group("public").pathsToMatch("/public/**").build();}@Beanpublic 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值@Autowiredprivate User user;// 注入@AutowiredMicroServiceUrl 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 getUserList() {List 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 getMap() {Map 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 getYMLConfig() {Map 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 getYMLConfigClass() {Map 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 getUser(@RequestBody @Parameter User user) { // 获取请求体Map map = new HashMap();map.put("user",user);return map;}}

遇到报错:

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

原因分析:

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

解决办法:

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

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

org.springdocspringdoc-openapi-ui新的版本号

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

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