JDK17踩坑
为什么要用jdk17?因为生产用了zgc,想要本地测试zgc参数,而mac/AArch64想要使用zgc,只能17版本,详情可以查看官网:https://wiki.openjdk.org/display/zgc/Main
各种unnamed module的报错
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @7791a895
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
at com.google.inject.internal.cglib.core.$ReflectUtils$1.run(ReflectUtils.java:52)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
at com.google.inject.internal.cglib.core.$ReflectUtils.<clinit>(ReflectUtils.java:42)
... 75 more
解决:增加如下vm参数
–add-opens java.base/java.lang=ALL-UNNAMED
–add-opens java.base/sun.net.util=ALL-UNNAMED
–add-opens java.base/java.util=ALL-UNNAMED
–add-opens java.base/java.lang.reflect=ALL-UNNAMED
–add-opens java.base/java.text=ALL-UNNAMED
–add-opens java.desktop/java.awt.font=ALL-UNNAMED
–add-opens java.desktop/sun.swing=ALL-UNNAMED
maven打包报错
报错如下
class lombok.javac.apt.LombokProcessor (in unnamed module @0x3ed27f49) cannot access class com.sun.tools.javac.processing.JavacProcessingEnvironment (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.processing to unnamed module @0x3ed27f49
原因:lombok版本过低
解决:引入更高版本
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>compile</scope>
</dependency>
Cacheable注解踩坑
语法错误
报错如下
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'notification' cannot be found on object of type 'org.springframework.cache.interceptor.CacheExpressionRootObject' - maybe not public or not valid?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:91)
at org.springframework.expression.spel.ast.OpMinus.getValueInternal(OpMinus.java:98)
at org.springframework.expression.spel.ast.OpMinus.getValueInternal(OpMinus.java:98)
配置如下
@Cacheable(key = "notification-all-category", cacheNames = "userSwitch")
原因:如果缓存的key是常量,需要使用单引号将常量包起来
解决方法:修改配置如下
@Cacheable(key = "'notification-all-category'", cacheNames = "userSwitch")
类型转换错误
报错如下
java.lang.ClassCastException: class java.lang.Boolean cannot be cast to class java.util.List (java.lang.Boolean and java.util.List are in module java.base of loader 'bootstrap')
at ....notification.service.template.TemplateService$$EnhancerBySpringCGLIB$$67b21dc9.selectAllValidShownCategoryCodes(<generated>)
at ....notification.controller.UserSwitchSettingController.queryUserSwitch(UserSwitchSettingController.java:123)
报错代码
@Cacheable(key = "'notification-all-category'", cacheNames = "userSwitch")
public List<TemplateCategoryEntity> selectAllValidShownCategoryCodes() {
return templateCategoryMapper.selectAllValidShownCategoryCodes();
}
检查Mapper响应类型没问题,那么就是缓存问题,查看userSwitch缓存配置为Boolean类型-.-!!!
新增缓存并配置匹配的序列化类型
private RedisCacheConfiguration getAllCategoryCacheConfiguration() {
return RedisCacheConfiguration
.defaultCacheConfig()
.serializeValuesWith(new RedisSerializationContext.SerializationPair<List<TemplateCategoryEntity>>() {
@Override
public RedisElementReader<List<TemplateCategoryEntity>> getReader() {
return byteBuffer -> JacksonUtil.fromList(new String(byteBuffer.array(), StandardCharsets.UTF_8 ),
TemplateCategoryEntity.class);
}
@Override
public RedisElementWriter<List<TemplateCategoryEntity>> getWriter() {
return o -> ByteBuffer.wrap(JacksonUtil.toJsonStr(o).getBytes(StandardCharsets.UTF_8));
}
})
.entryTtl(Duration.ofDays(30))
.prefixKeysWith("notification_category_");
}