SpringBoot 3.4.x踩坑记录及解决方案(持续更新)
废话
最近使用JDK17+Spring Boot3.4.0 做新项目遇到的一些坑,记录并且给出一些实际的解决方案
一、集成Mybatis Plus 3.5.9的问题
第一:不能只引入mybatis-plus-spring-boot3-starter依赖了,需要配合mybatis-plus-jsqlparser
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-jsqlparser</artifactId>
</dependency>
第二:版本统一管理
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-bom</artifactId>
<version>3.5.9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
详情请见:https://baomidou.com/getting-started/install/
二、集成Knife4j-OpenApi3
这个改动真的有点大,切费老鼻子劲了,不建议旧项目直接迁移,不然得改到S3赛季开战!
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
1、实体类写法变动
没有@ApiModel 和@ApiModelProperty写法,全局使用@Schema
@Schema(name = "OaDeptDTO对象", description = "部门表")
public class OaDeptDTO implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Schema(description = "主键")
private Long id;
@Schema(description = "父部门id",defaultValue = "0")
private Long parentId;
@Schema(description = "部门名称")
private String deptName;
@Schema(description = "部门编码")
private String deptCode;
@Schema(description = "状态(0正常 1停用)")
private Integer status;
}
2 接口类写法
没有@Api和@ApiOperation写法
旧版本 | 新版本 |
---|---|
@Api | @Tag |
@ApiOperationi | @Operation |
@Tag(name = "部门表")
@RestController
@RequiredArgsConstructor
@RequestMapping("/oa/dept")
public class OaDeptController {
private final OaDeptAppService oaDeptAppService;
@Operation(summary = "新增部门")
@PostMapping("/add")
public Result<Boolean> add(@RequestBody OaDeptDTO oaDept) {
return Result.ok(oaDeptAppService.add(oaDept));
}
}
注意注意请注意(呜呜呜~~~~)
Knife4j-OpenApi3存在一个大问题,如果你的项目中存在全局异常拦截类,@RestControllerAdvice/@ControllerAdvice,访问接口文档会报错,这是版本得bug,所以你无需自卑也不要过度伤心,叉掉你浏览器上密密麻麻的标签页吧。
详情见issue:https://github.com/xiaoymin/knife4j/issues/865
三、接口写法变动
之前的写法:(这样一直没毛病啊,但是3.4.0不行!!!!)
@Operation(summary = "查询数据明细")
@GetMapping("/{id}")
public Result<OaRoleVO> detail(@PathVariable Long id) {
return Result.ok(oaRoleAppService.detail(id));
}
报错信息:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.IllegalArgumentException: Name for argument of type [java.lang.Long] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.] with root cause
解决方案:
@Operation(summary = "查询数据明细")
@GetMapping("/{id}")
public Result<OaRoleVO> detail(@PathVariable("id") Long id) {
return Result.ok(oaRoleAppService.detail(id));
}