JAXB:类强制转换异常,但类具有相同的名称 [英] JAXB: class cast exception, but class has the same name

查看:129
本文介绍了JAXB:类强制转换异常,但类具有相同的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有趣的问题。

当我启动glassfish服务器时,每个人都可以正常工作。但是,我更改了一些代码并发布了服务器,并运行了我的客户端( SistemGirisClientKullaniciDogrula )。应用程序抛出此异常:

When I started glassfish server, everythings work fine. But, I changed some code and published the server, and I run my client (SistemGirisClientKullaniciDogrula). The application throws this exception:

java.lang.ClassCastException: tr.com.app.Kullanici cannot be cast to tr.com.app.Kullanici.

有趣的是,在Glassfish服务器重启后,应用程序正常运行。

Interesting part is, after the Glassfish server restart, application works fine.

我正在使用restlet-spring-hibernate。我还使用JAXB(org.restlet.ext.jaxb.jar)将XML转换为Java对象。我的应用程序服务器是Glassfish v3.0

I am using restlet-spring-hibernate. And I am also using JAXB (org.restlet.ext.jaxb.jar) for converting XML to Java objects. My application server is Glassfish v3.0

配置详细信息


  • restlet 2.0.5

  • spring 3.0.5

  • hibernate 3.3.2

  • glassfish v3.0

  • restlet 2.0.5
  • spring 3.0.5
  • hibernate 3.3.2
  • glassfish v3.0

客户类(仅供测试)

import java.io.IOException;

import org.restlet.Client;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Protocol;
import org.restlet.ext.jaxb.JaxbRepresentation;

public class SistemGirisClientKullaniciDogrula {

    public static void main(String[] Args) throws IOException {

        String url = "http://localhost:8080/Project/sistemgirisws";

        Client client = new Client(Protocol.HTTP);

        Kullanici kullanici = new Kullanici();
        kullanici.setKodu("1");

        JaxbRepresentation<Kullanici> jaxbRepresentationSendingKullanici= new JaxbRepresentation<Kullanici>(MediaType.APPLICATION_XML, kullanici);

        Request request = new Request(Method.GET, url, jaxbRepresentationSendingKullanici);
        Response response = client.handle(request);

        JaxbRepresentation<Kullanici> kullaniciResponse = new JaxbRepresentation<Kullanici>(response.getEntity(), Kullanici.class);
        kullanici = kullaniciResponse.getObject();

        System.out.println("kullanici id : " + kullanici.getId());
    }
}

网络服务

public class ProjectWebService {

/**
 * 
 * @param representation
 * @return
 */
@Get
public Representation getKullanici(Representation representation) {

    JaxbRepresentation<Kullanici> jaxbRepresentation = new JaxbRepresentation<Kullanici>(representation, Kullanici.class);

    Kullanici kullanici = new Kullanici();

    try {

        kullanici = jaxbRepresentation.getObject(); //THIS LINE THROW java.lang.classCastException tr.com.app.Kullanici cannot be cast to tr.com.app.Kullanici.

    } catch (IOException e) {
        e.printStackTrace();
    }

    try {

        kullanici = sistemGirisBusinessManager.kullaniciDogrula(kullanici);

        getResponse().setStatus(Status.SUCCESS_OK);
        return new JaxbRepresentation<Kullanici>(MediaType.APPLICATION_XML, kullanici);

    } catch (Exception exception) {

        exception.printStackTrace();
        getResponse().setStatus(Status.CLIENT_ERROR_EXPECTATION_FAILED);
        return new JaxbRepresentation<MesajList>(MediaType.APPLICATION_XML, sistemGirisBusinessManager.getMesajList());

    }
}
}

是否有人知道问题是什么吗?

Does Anyone know what the problem is?

推荐答案

这可能是一个类加载问题。在Java中,如果两个类加载器加载了相同的类,则将其视为两个不同的类。在这种情况下,您的转换将失败,因为在JVM中您似乎将一种类型转换为另一种类型而不在继承树中。

This could be a class loading issue. In Java, If two classloaders have loaded the same class, then it is treated as two different classes. In that case, your casting will fail because it seems to the JVM that you are casting one type to another which is not in an inheritance tree.

必须发生的是当你修改你的类时,它会被加载到不同的类加载器中,而web服务使用原始的类。

What must be happening is that when you modify your class, it gets loaded into a different classloader, where as the web service uses the original one.

这篇关于JAXB:类强制转换异常,但类具有相同的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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