我正在为用 Apache CXF 编写的可用 REST API(比如 API Y)编写一个包装 REST API(比如 API X)。对于包装器,我使用的是 CXF Webclient。这就是我从 X 中调用 Y 的方式。
@GET
@Path("{userName}")
public Response getUser(@PathParam("userName") String userName) {
try {
WebClient client
= WebClient.create("https://localhost:8080/um/services/um");
Response response = client.path("users/" + userName)
.accept(MediaType.APPLICATION_JSON)
.get();
User user = (User) response.getEntity();
return Response.ok(user).build();
} catch (Exception e) {
return handleResponse(ResponseStatus.FAILED, e);
}
}
在这里,用户类从 Y 复制到 X,因为我不能使用 Y 作为 X 的依赖项。唯一的区别是包名。现在,当我发送请求时,我在User user = (User) response.getEntity();
.
java.lang.ClassCastException: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream cannot be cast to org.comp.rest.api.bean.User
可能是因为类包名称不同?
有人可以帮我得到对用户对象的响应吗?