简体   繁体   中英

java.lang.Class cannot be cast to java.lang.String when invoking a EJB Bean Method

I've faced with the problem. I've successfully lookuped my remote bean from JBoss, but when I try to invoke any of its method next exception is about to appear:

java.lang.Class cannot be cast to java.lang.String.

Full stack trace is:

java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.String
at org.jboss.ejb.client.remoting.ProtocolMessageHandler.readAttachments(ProtocolMessageHandler.java:56)
at org.jboss.ejb.client.remoting.InvocationExceptionResponseHandler$MethodInvocationExceptionResultProducer.getResult(InvocationExceptionResponseHandler.java:85)
at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:212)
at org.jboss.ejb.client.TransactionInterceptor.handleInvocationResult(TransactionInterceptor.java:47)
at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:214)
at org.jboss.ejb.client.ReceiverInterceptor.handleInvocationResult(ReceiverInterceptor.java:96)
at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:203)
at org.jboss.ejb.client.EJBClientInvocationContext.awaitResponse(EJBClientInvocationContext.java:341)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:126)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:107)
at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:90)
at com.sun.proxy.$Proxy32.createEvent(Unknown Source)
at com.mypackage.FacadeImpl.createEvent(FacadeImpl.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)
at com.sun.proxy.$Proxy28.createEvent(Unknown Source)

Last row is the invocation of my method called "CreateEvent". I guess the exception is throwned inside "getResult()" method of EJBClientInvocationContext.

My code is:

IEventDao eventDao = (IEventDao) ctx.lookup("java:/task/EventDao!MyPackage.IEventDao");

EventDao has been successfully gotten from the remote. Trying to invoke a method:

public Event createEvent(final Event event) {
    return eventDao.createEvent(event);
}

Event event = new EventImpl("Test event: Star Wars: Episode VI");
createEvent(event); <-- Line #89. Exception has been throwned.

CreateEvent method body. But just for your info: it's not even enter this piece of code. It throwns the exception before even invoking entityManager.persist(event);

@Transactional(propagation = Propagation.REQUIRED)
public Event createEvent(Event event) {
    entityManager.persist(event);
    return event;
}

Here is the full code of FacadeImpl:

@Service
public class FacadeImpl implements BookingFacade {
@Autowired
private FacadeUtils facadeUtils;
private IEventDao eventDao;  
private InitialContext ctx;
private Properties props = new Properties();

public void init() throws NamingException, IOException {
    props.load(getClass().getResourceAsStream("/jndi.properties"));
    ctx = new InitialContext(props);
    eventDao = (IEventDao) ctx.lookup("java:/Assignment16-1.0-SNAPSHOT/EventDao!com.mypackage.IEventDao");      
}

public Event createEvent(final Event event) {
    return eventDao.createEvent(event);
}
}

Most of the time, the error java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.String is due to bad use of generics, or trying to downcast (eg Object -> String) to an incompatible type. Remember, you cannot assign an Object type to String type without explicit type-cast amd the object should be castable to String.

This is a good post for troubleshooting ClassCastException: http://examples.javacodegeeks.com/java-basics/exceptions/java-lang-classcastexception-how-to-solve-class-cast-exception/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM