[英]How to fix: “java.lang.ClassCastException: com.mashape.unirest.http.HttpResponse cannot be cast to org.apache.http.HttpResponse”
[英]ClassCastException com.sun.org.apache.xerces.internal.dom.ElementNSImpl
我正在收到ClassCastException。 我想知道我在做什么錯。 進行(RegistrationRequest)(element.getValue())
應該可以正常工作。 我知道(RegistrationRequest)element.getValue()
可能沒有。
SEVERE: Servlet.service() for servlet [Jersey REST Service] in context with path [/mCruiseOnCarPool4All] threw exception
java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to com.mcruiseon.common.message.request.RegistrationRequest
服務器發布方法,請注意方法簽名中的JAXBElement。 我正在對它clientSession.waitAndGetResponse((RegistrationRequest)(element.getValue())) ;
類型轉換clientSession.waitAndGetResponse((RegistrationRequest)(element.getValue())) ;
。 這是發生異常的行號。
@POST
@Path ("Request")
@Consumes({ MediaType.APPLICATION_JSON })
public Response post(JAXBElement<Object> element) {
AMessageStrategy response ;
try {
clientSession = new ClientSession(God.mCruiseOnServer) ;
} catch (InvalidServerDNSorIPException e) {
e.printStackTrace();
return Response.serverError().build() ;
}
sessionKey = sessionManager.setClientSession(clientSession) ;
clientSession.setSessionKey(sessionKey) ;
clientSession.getSendQueue().sendRequest((RegistrationRequest)(element.getValue()));
try {
response = clientSession.waitAndGetResponse((RegistrationRequest)(element.getValue())) ;
} catch (WaitedLongEnoughException e) {
return Response.serverError().build() ;
} catch (UnableToResolveResponseException e) {
return Response.serverError().build() ;
}
return Response.ok(response).build();
}
客戶端是一個junit測試用例,代碼的相關部分是
ClientIdentityConcrete clientIdentity = new ClientIdentityConcrete("username", "password", "secretkey") ;
RegistrationRequest register = new RegistrationRequest(clientIdentity);
String jsonStr = mapper.writeValueAsString(clientIdentity);
HttpPost request = new HttpPost("http://localhost:8081/mCruiseOnCarPool4All/carpool4all/Registration/Request");
StringEntity se = new StringEntity(jsonStr);
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
request.setEntity(se);
HttpResponse response = client.execute(request);
問題出在客戶端,junit測試用例。 我不是使用注冊創建Json對象,而是使用clientIdentity創建對象。 請注意writeValueAsString
。 我還修改了整個客戶端,以實際使用JSON客戶端api,而不是傳統的http。 目前還無法正常工作,我又遇到了一個例外。 但這是一個不同的問題。
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(UriBuilder.fromUri(
"http://localhost:8081/mCruiseOnCarPool4All").build());
ClientIdentityConcrete clientIdentity = new ClientIdentityConcrete("username", "password", "secretkey");
RegistrationRequest register = new RegistrationRequest(clientIdentity);
AMessageStrategy response = service.path("carpool4all").path("Registration").path("Request")
.type(MediaType.APPLICATION_JSON).post(RegistrationRequest.class, register);
assertNotNull(response) ;
assertNotNull(((RegistrationResponse)response).getIdentityHash()) ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.