我遇到过这个问题。我已经成功地从JBoss中查找了我的远程bean,但是当我尝试调用它的任何方法时,下一个异常即将出现:
java.lang.Class cannot be cast to java.lang.String.
完整堆栈跟踪是:
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)
最后一行是调用我的方法" CreateEvent"。 我猜这个例外是在#34; getResult()" EJBClientInvocationContext的方法。
我的代码是:
IEventDao eventDao = (IEventDao) ctx.lookup("java:/task/EventDao!MyPackage.IEventDao");
EventDao已成功从远程获取。 试图调用一个方法:
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方法体。但仅仅是为了您的信息:它甚至没有输入这段代码。它甚至在调用entityManager.persist(event);
@Transactional(propagation = Propagation.REQUIRED)
public Event createEvent(Event event) {
entityManager.persist(event);
return event;
}
以下是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);
}
}
答案 0 :(得分:0)
大多数情况下,错误java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.String
是由于使用泛型,或尝试向下转换(例如Object - &gt; String)为不兼容的类型。请记住,如果没有显式类型转换,则不能将Object类型分配给String类型,并且该对象应该可以转换为String。
这是排除ClassCastException问题的好帖子:http://examples.javacodegeeks.com/java-basics/exceptions/java-lang-classcastexception-how-to-solve-class-cast-exception/