java.lang.ClassCastException:无法将DTOObject强制转换为DTOObject [英] java.lang.ClassCastException: DTOObject cannot be cast to DTOObject

查看:424
本文介绍了java.lang.ClassCastException:无法将DTOObject强制转换为DTOObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行于Spring Boot 1.4.0M3的应用程序中遇到一个奇怪的问题,该应用程序使用Spring缓存实现,其中提供程序是Redis,在此我收到无法投射相同对象的classCastException

I am facing a weird issue in my application which runs on Spring Boot 1.4.0M3 which is using Spring cache implementation where provider is Redis where I receive classCastException that same object cannot be casted

我正在使用Mongodb作为数据库,并且我的User对象包含了延迟加载的Roles对象列表,而Roles内部包含的Permissions对象,如下所示

I am using Mongodb as database and I have User Object which contains List of Roles object loaded lazily and Roles internally contains Permissions Object like below

@Document
@Data
public class User implements Serializable{
private String passwordResetToken;

private boolean enabled = false;

@DBRef(lazy= true)
private List<Role> roleList;
}

我的角色DTO如下

@Data
@Document
public class Role implements Serializable{
   private String roleName;
    private String description;
    @DBRef(lazy= true)
    private List<Permission> permissions;
}

现在在我的春季MVC中,在加载所有角色时,我正在调用所有权限,并且由于这是重复性操作,因此我想到了缓存结果并使用redis,并在加载角色值时收到了以下异常消息.

Now in my spring MVC while loading all roles I am calling all permissions and since this is repetitive operation I thought of caching the result and using redis and while loading the roles value I receive below exception.

raised java.lang.ClassCastException: com.learning.securedapp.domain.Permission cannot be cast to com.learning.securedapp.domain.Permission

帮我克服这个错误.

我将源代码附加到我的项目中,并且在 RoleController.java

I am attaching the source code to my project and I receive error at line 91 of RoleController.java

要在本地环境中复制,请登录到应用程序,然后依次单击权限菜单和角色菜单,现在在角色"菜单中单击任何编辑图标.您将收到上述错误.

To Replicate in your local environment login to application and click on permissions menu and then roles menu, In Roles menu now click on any edit icon.you will receive above error.

推荐答案

在结合使用DevTools和缓存时,您需要了解

When you use DevTools with caching, you need to be aware of this limitation.

将对象序列化到缓存中时,应用程序类加载器为C1.然后,在更改了某些代码/配置之后,devtools会自动重新启动上下文并创建一个新的类加载器(C2).当您使用该缓存方法时,缓存抽象将在缓存中找到一个条目,然后从存储中反序列化该条目.如果缓存库未考虑上下文类加载器,则该对象将附加有错误的类加载器(这说明了奇怪的异常A cannot be cast to A).

When the object is serialized into the cache, the application class loader is C1. Then after you change some code/configuration, devtools automatically restart the context and creates a new classloader (C2). When you hit that cache method, the cache abstraction finds an entry in the cache and it deserializes it from the store. If the cache library doesn't take the context classloader into account, that object will have the wrong classloader attached to it (which explains that weird exception A cannot be cast to A).

TL;DR不会使用devtools序列化类.或将您的缓存库放入应用程序类加载器:

TL;DR do not serialize classes with devtools if the cache library doesn't use the context classloader. Or put your cache library in the application classloader:

restart.include.yourcache=/my-cache-lib-[\\w-]+\.jar

这篇关于java.lang.ClassCastException:无法将DTOObject强制转换为DTOObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆