访问用java编写的Web服务方法时的java.lang.ClassCastException。 JAXB [英] A java.lang.ClassCastException while accessing web service method written in java. jaxb

查看:120
本文介绍了访问用java编写的Web服务方法时的java.lang.ClassCastException。 JAXB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,我必须与MS SQL数据库进行交互。在我的应用程序中,我正在创建用于访问数据库表的Web服务(使用javax.jws)。我在Web服务中创建一个操作(方法),返回类型为java.lang.Object [] [],如下所示:

I am writing an application where i have to interact with MS SQL database. In my application i am creating web services (using javax.jws) for accessing database tables. i am creating one operation(method) in web service with return type java.lang.Object[][] as follows :

@WebMethod(operationName =get_HistoryInfoByUser)

@WebMethod(operationName = "get_HistoryInfoByUser")

public java.lang.Object[][] get_HistoryInfoByUser(@WebParam(name = "email_Id")
String email_Id) throws Exception{
   java.lang.Object[][] historyInfo = null;

     // some code here

  return historyInfo;
}

以及在我的应用程序中调用Web服务操作(方法),我写的以下
代码:

and for calling web service operation(method) in my application, i am writing following code:

public Object[][] get_HistoryInfoByUser(String email_Id) {

      java.util.List<net.java.dev.jaxb.array.AnyTypeArray> historyInfo = null;

    try {

        historyInfo = port.getHistoryInfoByUser(email_Id);

    } catch (Exception_Exception ex) {
        ex.printStackTrace();
    }
       return (Object[][]) historyInfo.toArray();
 }

但我得到例外


线程中的异常Thread-8
java.lang.ClassCastException:
[Ljava.lang.Object;不能转换为
[[Ljava.lang.Object;

Exception in thread "Thread-8" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [[Ljava.lang.Object;

Web服务操作返回类型是java.util.List (net.java.dev.jaxb.array.AnyTypeArray)
我需要返回类型java.lang.Object [] []。

Web service operation return type is java.util.List(net.java.dev.jaxb.array.AnyTypeArray) and i need return type java.lang.Object[][].

请你给我任何建议,这将有助于我克服这个问题。

Please can you give me any suggestion, which will help me to overcome with this problem.

推荐答案

转换不用于转换对象,它用于重新分类对象的类型。它告诉编译器应该将变量视为不同的类。如果您尝试将变量强制转换为与其不兼容的某种类型,则会得到 ClassCastException

Casting is not used for transforming objects, it's used for reclassifying the type of an object. It is there to tell the compiler that a variable should be treated like a different class. If you try to cast a variable to some type it is not compatible with, you get the ClassCastException.

Object ojc = new ArrayList();
obj.add(new Object()); //Compiler error because Object does not have an add method
ArrayList lst = (ArrayList)obj;
lst.add(new Object()); //Works because now the compiler knows that the variable is an ArrayList
MyClass myClass = (MyClass)obj; //ClassCastException because the object is not actually a MyClass object
myClass.add(new Object()); //Assuming MyClass defines and add method that takes and object, this line compiles

在你的情况下,Object []和Object [] []是完全不同的类型,而不是你可以在它们之间施放的东西。相反,您需要首先制作2D数组,然后将数组设置为2D数组的第一个成员。

In your case, Object[] and Object[][] are completely different types and not something that you can cast between. Instead, you need to first make the 2D array and then set your array to be the first member of the 2D array.

Object[][] result = new Object[1][];
result[0] = historyInfo.toArray();

这篇关于访问用java编写的Web服务方法时的java.lang.ClassCastException。 JAXB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆