4

I'm writing a wrapper REST API (say API X) for an available REST API (say API Y) written in Apache CXF. For the wrapper I'm using CXF Webclient. This is how 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, User class is copied from Y to X because I can't use Y as a dependency for X. The only difference is the package name. Now when I send a request, I get a class cast exception at 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

May be that's becuase of the class package name is different?

Can someone please help me to get the response to User object?

Bee
  • 12,251
  • 11
  • 46
  • 73

3 Answers3

2

Looks like your response is in JSON format, that right? You need to convert the stream of JSON bytes in the response to a Java Class. You are trying to cast the Stream Class to your User Class which obviously won't work. You need to parse the JSON from the data stream and then deserialize the JSON into your User Class. There are libraries that can help including Jackson and GSON

This guy has a simple example using Jackson ObjectMapper class - the ObjectMapper class has a readValue method that includes an InputStream parameter.

CodeClimber
  • 4,584
  • 8
  • 46
  • 55
  • Thanks for the answer. I already used GSON and got this done. – Bee Oct 23 '14 at 11:47
  • I thought there should be an implicit way in CXF to convert response to a java object, without using 3rd party libs. Hence the question. – Bee Oct 23 '14 at 11:49
  • 1
    Within CXF, you can convert XML to Java using JAXB but don't think there is anything for JSON. I stand to be corrected though. – CodeClimber Oct 23 '14 at 11:59
  • @CodeClimber: you can definitely convert JSON to Java when using CXF. By default, CXF uses Jettison to do the conversion. You can swap in other parsers like Jackson. Interestingly, you still use the JAXB annotations. – StvnBrkdll Jul 05 '16 at 17:06
1

Jackson provider is a solution:

 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);
spok
  • 91
  • 1
  • 2
0

Don't need to do anything additional.

If it is a GET method

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

If it is a POST method

TypeOfObject response = client.path("users/" + userName)
                              .accept(MediaType.APPLICATION_JSON)
                              .post(instatanceOfTypeOfObject, TypeOfObject.class);
GPrathap
  • 7,336
  • 7
  • 65
  • 83