我得到一個ClassCastException。我想知道我在這裏做錯了什麼。做(RegistrationRequest)(element.getValue())
應該已經工作了。我知道(RegistrationRequest)element.getValue()
可能沒有。ClassCastException com.sun.org.apache.xerces.internal.dom.ElementNSImpl
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())) ;
。這是我得到例外的行號。
@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);