Question

I am using Jersey client to interact with the Facebook Graph API. The Jersey client helps me parse JSON responses into Java classes.

Sometimes Facebook sends a 400 response along with useful information about the reason for the 400 response. For example: {"error":{"message":"Error validating application. Cannot get application info due to a system error.","type":"OAuthException","code":101}}

Jersey simply throws an exception and eats up the useful part of the response :-(

How do I get it to parse the JSON into a Java class with fields corresponding to the useful error information?

Was it helpful?

Solution 2

this code works:

try {
  User user = user_target
      .request()
      .get(User.class);
  System.out.println(user);
} catch (ClientErrorException primary_exception) {
  try {
    ErrorResponse error_response = primary_exception.getResponse().readEntity(ErrorResponse.class);
    System.out.println(error_response);
  } catch (ClientErrorException secondary_exception) {
    System.err.println("Secondary Exception: " + secondary_exception.getMessage());
    throw new ThisAppException("Secondary Exception", secondary_exception);
  }
}

OTHER TIPS

You can use ObjectMapper for that purpose. Basically, create an object that will match up with the content in the JSON and use readValue() method of the mapper to turn the JSON response into an object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top