How to create a Java object from a CXF Webclient response

I am writing a wrapper REST API (say, API X) for an accessible REST API (say, API Y) written in Apache CXF. For the shell, I use CXF Webclient. This is what I call Y from X.

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

Here the user class is copied from Y to X, because I cannot use Y as a dependency for X. The only difference is the package name. Now, when I submit a request, I get a class exception in 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

Maybe because the class package name is different?

Can someone help me get a response on a User object?

+4
source share
3 answers

, JSON, ? JSON Java-. Stream , , , . JSON , JSON . , Jackson GSON

Jackson ObjectMapper - ObjectMapper readValue, InputStream.

+2

Jackson - :

 List<Object> providers = new ArrayList<Object>();
 providers.add(new JacksonJaxbJsonProvider());
 WebClient client = WebClient.create("https://localhost:8080/um/services/um", providers);
 User user = client.get(User.class);
+1

.

GET

 TypeOfObject response = client.path("users/" + userName)
                              .accept(MediaType.APPLICATION_JSON)
                              .get(TypeOfObject.class);

POST

TypeOfObject response = client.path("users/" + userName)
                              .accept(MediaType.APPLICATION_JSON)
                              .post(instatanceOfTypeOfObject, TypeOfObject.class);
0

All Articles