Spingboot项目启动报错:Invalid value for MonthOfYear

本文解决了一个SpringBoot项目启动时报错的问题,错误信息为“Invalid value for MonthOfYear (valid values 1-12): 0”。通过调整spring-boot-maven-plugin的版本号从2.2.6.RELEASE或更高版本来解决该问题。

SpringBoot项目启动报错:Invalid value for MonthOfYear (valid values 1 - 12): 0

Exception in thread "main" java.time.DateTimeException: Invalid value for MonthOfYear (valid values 1 - 12): 0
	at java.time.temporal.ValueRange.checkValidValue(ValueRange.java:311)
	at java.time.temporal.ChronoField.checkValidValue(ChronoField.java:703)
	at java.time.LocalDate.of(LocalDate.java:267)
	at java.time.LocalDateTime.of(LocalDateTime.java:336)
	at org.springframework.boot.loader.jar.CentralDirectoryFileHeader.decodeMsDosFormatDateTime(CentralDirectoryFileHeader.java:130)
	at org.springframework.boot.loader.jar.CentralDirectoryFileHeader.getTime(CentralDirectoryFileHeader.java:119)
	at org.springframework.boot.loader.jar.JarEntry.<init>(JarEntry.java:55)
	at org.springframework.boot.loader.jar.JarFileEntries.getEntry(JarFileEntries.java:328)
	at org.springframework.boot.loader.jar.JarFileEntries.access$400(JarFileEntries.java:48)
	at org.springframework.boot.loader.jar.JarFileEntries$EntryIterator.next(JarFileEntries.java:379)
	at org.springframework.boot.loader.jar.JarFileEntries$EntryIterator.next(JarFileEntries.java:363)
	at org.springframework.boot.loader.jar.JarFile$2.nextElement(JarFile.java:198)
	at org.springframework.boot.loader.jar.JarFile$2.nextElement(JarFile.java:189)
	at org.springframework.boot.loader.archive.JarFileArchive$EntryIterator.next(JarFileArchive.java:186)
	at org.springframework.boot.loader.archive.JarFileArchive$EntryIterator.next(JarFileArchive.java:171)
	at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchives(JarFileArchive.java:84)
	at org.springframework.boot.loader.ExecutableArchiveLauncher.getClassPathArchives(ExecutableArchiveLauncher.java:70)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:49)
	at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)

假设项目打包后是app.jar,这个报错是发生在maven插件编译执行java -jar app.jar时,校验jar包时间月份报错。maven插件初始配置:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

由于引入了某个jar依赖使用的maven插件较低,和本地插件版本冲突,指定本地maven插件版本号,问题解决。加一下版本号,修改后的配置:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.2.RELEASE</version>
            </plugin>
        </plugins>
    </build>

启动成功!

PS:不一定非要使用上述版本,有的文章说是在2.2.6.RELEASE版本已经修复了这个问题,可以试下。

<think>好的,我现在需要解决用户在使用JDK21、Spring Boot 3.2.5和Mybatis-Plus升级后启动项目时遇到的错误:invalid value type for factoryObjectBeanType: java.lang.String。首先,我应该回顾用户提供的引用内容,尤其是引用[4],里面提到Spring Boot 3与Mybatis-Plus的版本兼容性问题,以及依赖冲突的问题。 用户的环境是JDK21和Spring Boot 3.2.5,所以需要确认Mybatis-Plus是否有对应支持这两个版本的稳定版本。根据引用[4]中的解决方案,Mybatis-Plus和Mybatis的依赖不能同时存在,而且Spring Boot 3对应的依赖包名称可能不同。用户可能还在pom.xml中同时引入了Mybatis和Mybatis-Plus,或者Mybatis-Plus的版本过低,导致不兼容。 首先,检查Mybatis-Plus的版本。Spring Boot 3.x需要Mybatis-Plus 3.5.5或更高版本。用户应该确保使用的是这个版本或更新的。然后,确认依赖配置是否正确,特别是是否有重复的Mybatis依赖。比如,是否同时引入了mybatis-spring-boot-starter和mybatis-plus-spring-boot3-starter,这会导致冲突。 另外,引用[4]提到,Spring Boot 3对应的Mybatis-Plus依赖包名可能不同。例如,正确的依赖应该是mybatis-plus-spring-boot3-starter,而不是之前的mybatis-plus-boot-starter。如果用户使用的是旧的依赖名称,需要更新为适用于Spring Boot 3的版本。 接下来,可能需要检查项目的依赖树,排除掉可能存在的旧版本或冲突的依赖。使用Maven的dependency:tree命令可以帮助识别问题。如果存在其他库间接引入了Mybatis的老版本,也需要排除。 最后,确保代码中没有同时使用Mybatis和Mybatis-Plus的注解或配置,这可能导致工厂Bean类型不匹配。例如,@MapperScan的包是否正确指向Mybatis-Plus的组件。 总结步骤:升级Mybatis-Plus到兼容版本,检查依赖冲突,更新依赖名称,排除旧依赖,清理并重新构建项目。如果用户按照这些步骤操作,应该能解决启动报错的问题。</think>### 解决方案分步说明 #### 1. 确认MyBatis-Plus版本兼容性 - **必须使用MyBatis-Plus 3.5.5以上版本**,推荐使用最新稳定版(当前最新为3.5.6) - Spring Boot 3.2.x需配合`mybatis-plus-spring-boot3-starter`依赖包 ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-spring-boot3-starter</artifactId> <version>3.5.6</version> </dependency> ``` #### 2. 排除冲突依赖 - 检查`pom.xml`中是否同时存在以下冲突依赖: - `mybatis-spring-boot-starter` - `mybatis-plus-boot-starter`(旧版命名) - 移除所有MyBatis原生依赖,保留**唯一**的MyBatis-Plus依赖 #### 3. 验证JDK兼容性 - JDK21需配合MyBatis-Plus 3.5.5+版本 - 检查项目编译配置: ```xml <properties> <java.version>21</java.version> <maven.compiler.source>21</maven.compiler.source> <maven.compiler.target>21</maven.compiler.target> </properties> ``` #### 4. 清理构建缓存 执行Maven强制清理并重新编译: ```bash mvn clean install -U ``` #### 5. 验证注解配置 检查启动类`@MapperScan`注解是否正确引用MyBatis-Plus包: ```java @MapperScan("com.yourpackage.mapper") // 无需指定具体实现类 ``` #### 6. 检查配置文件 确保没有混合使用MyBatis原生配置项(如`mybatis.configuration.*`),应使用MyBatis-Plus专属配置: ```yaml mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl ``` ### 版本对照表 | 组件 | 要求版本 | 错误版本示例 | |---------------|-----------------------|-------------------| | Spring Boot | 3.2.x | 2.x.x | | MyBatis-Plus | 3.5.5+ | 3.4.x | | JDK | 17+ | 8/11 | ### 典型错误配置示例 ```xml <!-- 错误示例:混合使用旧版依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <!-- 必须移除 --> <version>2.3.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <!-- 旧版命名 --> <version>3.4.3</version> </dependency> ``` ### 验证成功的配置示例 ```xml <!-- 正确示例:Spring Boot 3专用依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-spring-boot3-starter</artifactId> <version>3.5.6</version> </dependency> ``` ### 技术原理说明 该错误本质是MyBatis的`SqlSessionFactoryBean`未能正确识别类型元数据。MyBatis-Plus 3.5.5+版本针对Spring Boot 3.x重构了自动配置逻辑,通过`MybatisPlusAutoConfiguration`类重写了`sqlSessionFactory`方法,确保类型安全注入[^4]。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值