File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Why i can't cast my object properly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Why i can Watch "Why i can New topic
Author

Why i can't cast my object properly?

Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 184
Good day,

I'm working on implementation of an external vendor API, which is in fact an EJB service and package into an .ear file.

the idea is first we call to API manager, and then use the method which return a list of object, the object is belong to API proprietary object (ie:Status object).

What have been done so far, i managed to get the API manager which is not null, and the manager return a list, however it seem return me a list in different object type, as mentioned below, we expecting "Status" object but it return StatusImpl.

the API manager which is local session bean give the correct result if using stateless bean to implement the method of it, but not the case if we use Spring to do it.

Appreciated anyone can shed some light on this, what is wrong and how can i handle it ? Thanks a lot!

Below is the exception:

servlet springDispatcherServlet threw exception
java.lang.ClassCastException: com.hp.api.schoolclass.StatusImpl cannot be cast to com.api.schoolclass.Status


Below code managed to get the API manager



Below is how my own manager to call the API method


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Just a wild guess, do you have a copy of Status class in your codebase or are you referring to the Status class from the EAR's jar of the EJB module?


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 184
Thanks for reply Ankit, I need to put all necessary api .jar files as dependency in maven, the Status class considering in my code base right?

Appreciated your sharing what will happen if Status class in my codebase or EAR's jar.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

I'm not sure what the structure of your project is, but what I meant is there should not be two versions of Status class. I guess your web application in which you are using Spring to get the session bean and the EJB application is part of the same EAR. What I'm trying to say is there should not be two separate Status.java files, one in your EJB project and one in your WAR (Web application) project. The WAR project should have a dependency on EJB project's JAR to refer to Status class...
Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 184
Hi Ankit,

Sound like making sense to me, we are using war project and we included the api jar file into our environment, we imported it in our client class, Let called this as "Local" Status.class, we then do a Context lookup to the ear file, we get the EJB manager and invoked it method and return result and finally assign back into "Local" Status.class.

I might be new to EJB, or what is the right approach i should go for? how my war project do the dependency to EAR project's jar ? appreciated if you have little snippet code to enlighten me if possible.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

I'm still a little confused how everything is setup in your project. Let's take this one step at a time.

1. Are the EJB and WAR project are in the same EAR?
_I. If they are:
___a. You must have a compile time dependency on the EJB project.
___b. At runtime the Status.class would be available to the web application from the EJB application as it's running as part of the same EAR application.
_II. If the EJB and WAR are not part of the same EAR (I don't think that would be the case as you said your EJBs are local EJBs)
___a. In this case you'll have to package EJB project's JAR with your WAR project.
2. Are you using Maven? If yes maybe you can post your maven configuration how you've defined the dependencies...
Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 184
Hi Ankit,

Sorry for confusion, The EJB and WAR are not part of same EAR.

means in the EJB which call the API method are package into EAR named "api-ear", we have a WAR to call this "api-ear" to get the value.

But both EAR and WAR under the same jboss server. so i have to take the EJB JAR and package together with my WAR ? is there a better choice? caused the EAR is provided by vendor. we try not to modify what they given.
Nakataa Kokuyo
Ranch Hand

Joined: Jul 24, 2011
Posts: 184
Thanks Ankit for your help, found out it was due to duplicate API jar file.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why i can't cast my object properly?
 
Similar Threads
Create Immutable HashMap
Could EJB work with JPA and Hibernate
SCBCD 5 - Exam Objectives
I have deployed my ejb with Weblogic and I have a problem with my client
NamingException 'null' with lookup of entiy bean