加载网页时出现java.lang.ClassNotFoundException: org.unbescape.uri.UriEscape异常
解决
java.lang.ClassNotFoundException: org.unbescape.uri.UriEscape异常 这个错误很典型——你用Thymeleaf的URL表达式th:action="@{/competitions/{id}(id=${competition.id})}"时,Thymeleaf需要依赖org.unbescape来处理URI转义,但它找不到对应的类。虽然你已经在pom.xml里加了依赖,但大概率是版本不兼容、依赖没生效或者冲突导致的,试试下面的解决方案:
匹配Thymeleaf与unbescape的版本
不同版本的Thymeleaf对unbescape的版本有明确要求:- Thymeleaf 3.1.x系列:建议使用
unbescape 1.1.6.RELEASE及以上 - Thymeleaf 3.0.x系列:对应
unbescape 1.1.x系列
你可以先确认项目中Thymeleaf的版本,然后调整pom.xml里的unbescape依赖。比如Thymeleaf 3.1.x的配置:
<dependency> <groupId>org.unbescape</groupId> <artifactId>unbescape</artifactId> <version>1.1.6.RELEASE</version> </dependency>- Thymeleaf 3.1.x系列:建议使用
检查依赖作用域
确保你的unbescape依赖没有设置test或provided作用域——这两个作用域会导致依赖在运行时不被加载。如果没写scope,默认就是compile,是正常的。清理并重建项目
Maven缓存有时候会抽风,导致依赖没正确下载或生效。执行以下命令强制清理缓存并重新构建:mvn clean install -U或者在IDE里右键项目,选择「Maven」→「Reload Project」刷新依赖。
排查依赖冲突
可能有其他第三方依赖间接引入了旧版本的unbescape,覆盖了你添加的版本。用下面的命令查看依赖树,找出冲突的来源:mvn dependency:tree | grep unbescape如果发现多个版本的unbescape,在冲突的依赖里添加排除规则,比如:
<dependency> <groupId>冲突依赖的groupId</groupId> <artifactId>冲突依赖的artifactId</artifactId> <exclusions> <exclusion> <groupId>org.unbescape</groupId> <artifactId>unbescape</artifactId> </exclusion> </exclusions> </dependency>
内容的提问来源于stack exchange,提问作者Lucia
相关产品推荐
相关产品推荐

