Json ClassCastException com.sun.org.apache.xerces.internal.dom.ElementNSImpl
我得到一个ClassCastException。我不知道我在这里做错了什么。执行Json ClassCastException com.sun.org.apache.xerces.internal.dom.ElementNSImpl,json,jakarta-ee,jersey,classcastexception,Json,Jakarta Ee,Jersey,Classcastexception,我得到一个ClassCastException。我不知道我在这里做错了什么。执行(RegistrationRequest)(element.getValue())应该可以工作。我知道(RegistrationRequest)元素.getValue()可能没有 SEVERE: Servlet.service() for servlet [Jersey REST Service] in context with path [/mCruiseOnCarPool4All] threw exception
(RegistrationRequest)(element.getValue())
应该可以工作。我知道(RegistrationRequest)元素.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())代码>。这是我得到异常的行号
@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测试用例上。我没有使用register为创建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()) ;
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()) ;