2011-05-31 51 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

相關問題