springboot 3.5.0 jpa使用jpql的时候如 “select u from User as u”,会报错如
org.hibernate.query.SyntaxException: At 1:** and token ‘)’, no viable alternative at input ‘select new User(*) ,改返回上个版本3.4.6正常
分类:java
多个@ControllerAdvice控制异常处理排序
环境Springboot3,多个@ControllerAdvice 发现当有比如异常AException,AException继承了Exception时。异常没有由 AException 的处理逻辑处理,测试这两个方式都是有效的:
1:@ComponentScan(basePackages={“处理 AException 所在目录”}) 与加载顺序有关,需要把 AException 处理类优先加载
2:@Order 注解,需要放在类上!放方法上是无效的。优先级为正序,如0优先于1。默认值为Int最大值。
感觉用@Order好一点
springboot3.4.x jpa中ddl_auto需要注意时间格式
老项目是在springboot2时创建的mysql表有的字段用的date格式,但程序没有加jpa的@Column注解,导致程序升级到springboot3的时候时间格式变成了datetime,一些依赖时间的判断出错了。
项目升级需要注意下mysql中date格式,需要补齐@Column注解
springboot3.2+版本跟3.1在ClassLoader中的差异
通过jar运行,读取url时候,老版本路径是/BOOT-INF/classes, 新版本是BOOT-INF/classes 少了一个字符
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project xxx: Fatal error compiling: error: invalid target release: 17 -> [Help 1]
使用jenkins做单元测试的时候出现的异常
项目环境是jdk17,本地测试,测试服运行都没有问题,只有单元测试出异常。原因是maven中节点环境变量的JAVA_HOME指向的JDK版本是/usr/lib/jvm/openjdk,我的服务器是11,改成正确的路径就好了
开始以为是maven-compiler-plugin 版本有对应关系,不匹配。
因为命令行直接打java -version是正确的版本17,没有发现问题
springboot+mysql emoji存储异常
测试服运行正常,正式服无法存储
尝试了网上的方法
1、mysql链接后加 characterEncoding参数,无效
2、数据库字段原来也是utf8mb4改成了utf8mb4_unicode ,无效
最终有效果的是:
升级mysql依赖版本 从mysql-connector-java改成 mysql-connector-j的最新版,有效了。
有相似情况的可以试试
Caused by: com.oracle.svm.core.jdk.UnsupportedFeatureError: No classes have been predefined during the image build to load from bytecodes at runtime.
spirngboot项目,graalvm native 编译完成后运行服务运行报错,
Caused by: com.oracle.svm.core.jdk.UnsupportedFeatureError: No classes have been predefined during the image build to load from bytecodes at runtime.
看帖子说是jdk版本问题,可我是用的graalvm编译的没错呀。后测试发现跟springboot版本有关,springboot3.1.0会报这个错,3.0.4和3.0.7测试过没问题
反序列化错误com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `***` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
在junit中用jackson做反序列化的时候报错
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of
***
(no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
原因是用了2个class继承一个interface,反序列化interface时无法找到用哪个class结果出了异常,网上能搜到的解决方法是写个配置类,如:@Bean注解创建ObjectMapper、重写configureMessageConverters方法。
发现了一个更简单的方式,是在属性中加@JsonDeserialize注解如:
//这里I是interface I2是I的实现类 @JsonDeserialize(contentAs=I2.class) public interface I{ Integer a; }
这样Junit中的反序列化就能跑通了
maven提交中央仓库需要注意的地方
标准流程网上有很多跟着做就行,注意的点有几个
1.groupId要留意别写错,有时提示403可能不是账号问题是groupId没对上
2.gpg公钥上传服务器,我的win10命令行上传提示失败,结果发现公钥的网页可以支持文件上传
3.deploy成功发布后release是删除不了,所以release版本慎重点发布,开发的时候用SNAPSHOT版本就很好。release版本是可以在mvnrepository.com搜索到的。
hql组合条件查sum的方法
@Query("select sum(r.amount) from RechargeOrder r where 1=1 " + "AND (case when (:status is null) then true when(:status=r.status) then true else false end)=true " + "AND (case when (:walletType is null) then true when(:walletType=r.walletType) then true else false end)=true " + "AND (case when (:payType is null) then true when(:payType=r.payType) then true else false end)=true " + "AND (case when (:userId is null) then true when(:payType=r.userId) then true else false end)=true " + "AND (case when (:rechargeType is null) then true when(:rechargeType=r.rechargeType) then true else false end)=true " + "AND (case when (:successTimeBegin is null) then true when(:successTimeBegin<=r.successTime) then true else false end)=true " + "AND (case when (:successTimeEnd is null) then true when(:successTimeEnd>=r.successTime) then true else false end)=true " + "") Integer sumAmount( @Param("status") Integer status ,@Param("walletType") Integer walletType ,@Param("payType") Integer payType ,@Param("userId") Integer userId ,@Param("rechargeType") Integer rechargeType ,@Param("successTimeBegin") Date successTimeBegin ,@Param("successTimeEnd") Date successTimeEnd );