我有一個有趣的問題。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());
}
}
}
有誰知道問題是什麼?
嗨Yohan,謝謝你的回覆。但是我只有一場戰爭。一場戰爭可能會使用兩個不同的類裝載器。 – 2011-05-31 07:51:17
Hi @Musa,是的,即使只有一個WAR文件,也可以在容器中使用多個類加載器。這意味着,容器只能爲WAR文件使用單獨的類加載器。我的猜測**是,當你部署應用程序時,它使用一個類加載器,並且爲了後續修改(調試模式),代碼替換使用另一個類加載器。 – 2011-05-31 07:55:09
@Musa YUVACI - 此表單的ClassLoader泄漏通常是由於服務器範圍ClassLoader中的引用(應用程序服務器中的錯誤或開發人員在WARs/EAR之外添加庫)。 – McDowell 2011-05-31 09:08:22