2011-05-31 53 views
3

我有一个有趣的问题。JAXB:类抛出异常,但类具有相同的名称

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

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

有趣的是,Glassfish服务器重新启动后,应用程序工作正常。

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

详细的congiguration

  • 的Restlet 2.0.5
  • 春天3.0.5
  • 冬眠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()); 
    } 
} 

Web服务

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

    } 
} 
} 

有谁知道问题是什么?

回答

3

这可能是一个类加载问题。在Java中,如果两个类加载器加载了相同的类,那么它将被视为两个不同的类。在这种情况下,您的投射将失败,因为JVM认为您将一种类型投射到不属于继承树的另一种类型。

必须发生的事情是,当您修改您的类时,它将被加载到不同的类加载器中,其中Web服务使用原始类。

+0

嗨Yohan,谢谢你的回复。但是我只有一场战争。一场战争可能会使用两个不同的类装载器。 – 2011-05-31 07:51:17

+0

Hi @Musa,是的,即使只有一个WAR文件,也可以在容器中使用多个类加载器。这意味着,容器只能为WAR文件使用单独的类加载器。我的猜测**是,当你部署应用程序时,它使用一个类加载器,并且为了后续修改(调试模式),代码替换使用另一个类加载器。 – 2011-05-31 07:55:09

+2

@Musa YUVACI - 此表单的ClassLoader泄漏通常是由于服务器范围ClassLoader中的引用(应用程序服务器中的错误或开发人员在WARs/EAR之外添加库)。 – McDowell 2011-05-31 09:08:22

2

确实在不同的类装载机中存在问题。我也有过,并且为那些对象添加了implements SerializableserialVersionUID解决了问题。

+0

嗨Vidmantas,您添加的serialVesionUID的值是什么?我有我的所有实体类实现Serializable和serialVersionUIDs(都具有值= 1L)。我仍然有与铸造异常的问题,虽然它发生非常难以预测。 – greenskin 2013-04-23 12:36:23

相关问题