java.lang.ClassCastException:java.lang.String无法强制转换为com.tutorial.stateful.Book

时间:2016-04-22 11:26:56

标签: java ejb-3.1

因为我试图测试一个有状态的EJB,我得到了" java.lang.ClassCastException:java.lang.String无法强制转换为com.tutorial.stateful.Book"错误。

我的客户端代码如下所示

 import com.tutorial.stateful.LibraryStatefulSessionBeanRemote;
import com.tutorial.stateful.Book;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class EJBTesterStateful {

    BufferedReader brConsoleReader = null; 
   Properties props;
   InitialContext ctx;
   {
      props = new Properties();
      try {
         props.load(new FileInputStream("jndi.properties"));
      } catch (IOException ex) {
         ex.printStackTrace();
      }
      try {
         ctx = new InitialContext(props);            
      } catch (NamingException ex) {
         ex.printStackTrace();
      }
      brConsoleReader = 
      new BufferedReader(new InputStreamReader(System.in));
   }

   public static void main(String[] args) {

      EJBTesterStateful ejbTester = new EJBTesterStateful();

      ejbTester.testStatefulEjb();
   }

   private void showGUI(){
      System.out.println("**********************");
      System.out.println("Welcome to Book Store");
      System.out.println("**********************");
      System.out.print("Options \n1. Add Book\n2. Exit \nEnter Choice: ");
   }

   private void testStatefulEjb(){

      try {
         int choice = 1; 

         LibraryStatefulSessionBeanRemote libraryBean =
         (LibraryStatefulSessionBeanRemote)ctx.lookup("ejb:/EjbComponent//LibraryStatefulSessionBean!com.tutorial.stateful.LibraryStatefulSessionBeanRemote?stateful");

         while (choice != 2) {
            String bookName;
            showGUI();
            String strChoice = brConsoleReader.readLine();
            choice = Integer.parseInt(strChoice);
            if (choice == 1) {
               System.out.print("Enter book name: ");
               bookName = brConsoleReader.readLine();
               Book book = new Book();
               book.setName(bookName);
               libraryBean.addBook(book.toString());          
            } else if (choice == 2) {
               break;
            }
         }

         List<Book> booksList = libraryBean.getBooks();

       System.out.println("Book(s) entered so far: " + booksList.size());
        int i = 0;

        // Exception line-trigger         
        for(Book book:booksList){
         System.out.println((i+1)+". " + book.getName());
         }

         LibraryStatefulSessionBeanRemote libraryBean1 = 
            (LibraryStatefulSessionBeanRemote)ctx.lookup("ejb:/EjbComponent//LibraryStatefulSessionBean!com.tutorial.stateful.LibraryStatefulSessionBeanRemote?stateful");
         List<String> booksList1 = libraryBean1.getBooks();
         System.out.println(
            "***Using second lookup to get library stateful object***");
         System.out.println(
            "Book(s) entered so far: " + booksList1.size());
         for ( i = 0; i < booksList1.size(); ++i) {
            System.out.println((i+1)+". " + booksList1.get(i));
         }       
      } catch (Exception e) {
         System.out.println(e.getMessage());
         e.printStackTrace();
      }finally {
         try {
            if(brConsoleReader !=null){
               brConsoleReader.close();
            }
         } catch (IOException ex) {
            System.out.println(ex.getMessage());
         }
      }
   }

}

 the statefull ejb class looks like:

    @Stateful
public class LibraryStatefulSessionBean implements LibraryStatefulSessionBeanRemote{

       List<String> bookShelf;    

   public LibraryStatefulSessionBean(){
      bookShelf = new ArrayList<String>();
   }

   public void addBook(String bookName) {
      bookShelf.add(bookName);
   }    

   public List<String> getBooks() {
      return bookShelf;
   }
}

可以解决这个铸造问题。

非常感谢

谢谢,您的响应有助于避免在有状态Bean类中发生某些更改后出现转换问题:

2 个答案:

答案 0 :(得分:1)

>>> from __future__ import print_function >>> print("These \n linebreaks don't work:\n", 1) These linebreaks don't work: 1 返回getBooks字符串而不是图书

List

答案 1 :(得分:0)

如果您看到以下错误消息

java.lang.String cannot be cast to com.tutorial.stateful.Book

很明显,您要将一个String的对象分配给一个Book的对象。这是不可能的,因为这两个类之间没有关系。因此,String类无法转换为Book类型。

它会引发ClassCastException

问题出现在这行代码

List<Book> booksList = libraryBean.getBooks(); //this line will throw an exception, because it returns a List<String> not List<Book>.

// Exception line-trigger         
for(Book book: booksList){
    System.out.println((i+1) + ". " + book.getName());
}

,因为

public List<String> getBooks() { //returns a List<String> not List<Book>.
    return bookShelf;
}