最近在用devtools时,碰到个神奇的错误
java.lang.ClassCastException: cn.mirakyux.devmgr.main.system.user.model.User cannot be cast to cn.mirakyux.devmgr.main.system.user.model.User
我的心情就像突然被通知要怎么证明你是你自己一样,查了查资料,说是ClassLoader的问题,网上的大神们给了两种方案,放弃是不可能放弃使用devtools的
解决:
方案一、解决方案就是在resources目录下面创建META-INF文件夹,然后创建spring-devtools.properties文件,文件加上类似下面的配置:
restart.exclude.companycommonlibs=/mycorp-common-[\w-]+.jar
restart.include.projectcommon=/mycorp-myproj-[\w-]+.jar
All property keys must be unique. As long as a property starts with
restart.include. or restart.exclude. it will be considered. All
META-INF/spring-devtools.properties from the classpath will be loaded.
You can package files inside your project, or in the libraries that
the project consumes.
方案二、不使用spring-boot-devtools
然额!不知道是不是我姿势不太对,怎么研究也没弄出来!
所以就想了个折中的方法
public static User getUser()
{
Object obj = getSubjct().getPrincipal();
User user = new User();
if(obj instanceof User) {
user = (User) obj;
} else {
user = JSON.parseObject(JSON.toJSON(obj).toString(), User.class);
}
// return (User) getSubjct().getPrincipal();
return user;
}
至少能用了