简体   繁体   English

调用EJB Bean方法时,不能将java.lang.Class强制转换为java.lang.String

[英]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: 我已经成功地从JBoss查找了我的远程bean,但是当我尝试调用其任何方法时,下一个异常将出现:

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". 最后一行是调用我的方法“ CreateEvent”。 I guess the exception is throwned inside "getResult()" method of EJBClientInvocationContext. 我猜想该异常抛出在EJBClientInvocationContext的“ getResult()”方法内。

My code is: 我的代码是:

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

EventDao has been successfully gotten from the remote. 已成功从远程获取EventDao。 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. CreateEvent方法主体。 But just for your info: it's not even enter this piece of code. 只是为了您的信息:它甚至都不输入这段代码。 It throwns the exception before even invoking entityManager.persist(event); 它甚至在调用entityManager.persist(event);之前引发异常entityManager.persist(event);

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

Here is the full code of FacadeImpl: 这是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. 在大多数情况下,错误java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.String强制转换java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.String是由于对泛型的错误使用或试图向下转换(例如Object-> String)为不兼容的类型所致。 Remember, you cannot assign an Object type to String type without explicit type-cast amd the object should be castable to String. 请记住,如果没有显式类型转换并且对象应该可转换为String,则不能将Object类型分配为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/ 这是对ClassCastException进行故障排除的好文章: http : //examples.javacodegeeks.com/java-basics/exceptions/java-lang-classcastexception-how-to-solve-class-cast-exception/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 引导中的“无法解析方法‘run(java.lang.Class, java.lang.String[])’” - "Cannot resolve method 'run(java.lang.Class, java.lang.String[])'" in Spring Boot 在java.lang.Class中找不到错误getMethod(java.lang.String,java.lang.Class,java.lang.Class) - Error getMethod(java.lang.String,java.lang.Class,java.lang.Class) not found in java.lang.Class java.lang.Class 在日期捕获和字符串格式化时不能转换为 clojure.lang.IFn - java.lang.Class cannot be cast to clojure.lang.IFn when date capturing and string formatting [java.lang.String中; 无法强制转换为java.lang.String - [java.lang.String; cannot be cast to java.lang.String 尝试在空对象引用上调用虚拟方法&#39;java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String)&#39; - Attempt to invoke virtual method 'java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String)' on a null object reference IntelliJ:无法解析方法 'run(java.lang.Class, String[])' - IntelliJ : Cannot resolve method 'run(java.lang.Class, String[])' JAVA“无法转换为java.lang.String” - JAVA “cannot be cast to java.lang.String” serverError:类java.lang.ClassCastException java.lang.Integer无法转换为java.lang.String - serverError: class java.lang.ClassCastException java.lang.Integer cannot be cast to java.lang.String 无法将java.lang.String强制转换为HQL - java.lang.String cannot be cast HQL 无法转换为java.lang.String异常 - cannot be cast to java.lang.String exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM