gpt4 book ai didi

java - JAXB:类转换异常,但类具有相同的名称

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:56:24 26 4
gpt4 key购买 nike

我有一个有趣的问题。

当我启动 glassfish 服务器时,一切正常。但是,我更改了一些代码并发布了服务器,然后运行我的客户端 (SistemGirisClientKullaniciDogrula)。应用程序抛出此异常:

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

网络服务

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

}
}
}

有人知道问题出在哪里吗?

最佳答案

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

一定会发生的事情是,当您修改您的类时,它会被加载到一个不同的类加载器中,而 Web 服务使用的是原始类加载器。

关于java - JAXB:类转换异常,但类具有相同的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6184313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com