JAXB: class cast exception, but class has the same name

早过忘川 提交于 2019-12-10 19:29:25

问题


I have a interesting problem.

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.

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

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

detail for congiguration

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

client class(Just for test)

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());
    }
}

Web Service

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?


回答1:


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.

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.




回答2:


Problem indeed in different class loaders. i've had it too and adding implements Serializable and serialVersionUID for those object solved the problem.



来源:https://stackoverflow.com/questions/6184313/jaxb-class-cast-exception-but-class-has-the-same-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!