From: Kai Moritz Date: Sat, 7 Nov 2015 10:14:16 +0000 (+0100) Subject: Added JSON-mapping for responses from the Graph-API X-Git-Tag: facebook-utils-2.5.0~10 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=d0accf7044000e7f20e8bd586c3c48ce8c8cef7d;p=facebook-utils Added JSON-mapping for responses from the Graph-API Take a look at the Graph-API documentation: https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#errors --- diff --git a/pom.xml b/pom.xml index 2543a7d..f4bd89c 100644 --- a/pom.xml +++ b/pom.xml @@ -142,12 +142,19 @@ provided - + + + com.fasterxml.jackson.core + jackson-core + ${jackson.version} + com.fasterxml.jackson.core jackson-databind ${jackson.version} + + org.springframework.security spring-security-core diff --git a/src/main/java/de/juplo/facebook/exceptions/GraphApiException.java b/src/main/java/de/juplo/facebook/exceptions/GraphApiException.java index 03aa627..1055b20 100644 --- a/src/main/java/de/juplo/facebook/exceptions/GraphApiException.java +++ b/src/main/java/de/juplo/facebook/exceptions/GraphApiException.java @@ -1,5 +1,8 @@ package de.juplo.facebook.exceptions; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.annotation.JsonRootName; import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; /** @@ -59,4 +62,30 @@ public class GraphApiException extends OAuth2Exception builder.append("}}"); return builder.toString(); } + + + /** + * This class represents an error message from the Graph-API + * + * @see https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#errors + */ + @JsonRootName("error") + @JsonPropertyOrder({ "message", "type", "code", "error_subcode", "error_user_title", "error_user_msg", "fbtrace_id" }) + public static class FacebookErrorMessage + { + @JsonProperty("message") + String message; + @JsonProperty("type") + String type; + @JsonProperty("code") + Integer code; + @JsonProperty("error_subcode") + Integer subCode; + @JsonProperty("error_user_title") + String userTitle; + @JsonProperty("error_user_msg") + String userMessage; + @JsonProperty("fbtrace_id") + String traceId; + } } diff --git a/src/test/java/de/juplo/facebook/exceptions/FacebookErrorMessageMappingTest.java b/src/test/java/de/juplo/facebook/exceptions/FacebookErrorMessageMappingTest.java new file mode 100644 index 0000000..cd1a4c8 --- /dev/null +++ b/src/test/java/de/juplo/facebook/exceptions/FacebookErrorMessageMappingTest.java @@ -0,0 +1,72 @@ +package de.juplo.facebook.exceptions; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import de.juplo.facebook.exceptions.GraphApiException.FacebookErrorMessage; +import java.io.IOException; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * @author Kai Moritz + */ +public class FacebookErrorMessageMappingTest +{ + final String example = + "{" + + "\"error\":{" + + "\"message\":\"Message describing the error\"," + + "\"type\":\"OAuthException\"," + + "\"code\":190," + + "\"error_subcode\":460," + + "\"error_user_title\":\"A title\"," + + "\"error_user_msg\":\"A message\"," + + "\"fbtrace_id\":\"EJplcsCHuLu\"" + + "}" + + "}"; + + ObjectMapper mapper; + + + @Before + public void setUp() + { + mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true); + mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true); + } + + + @Test + public void testSerialize() throws JsonProcessingException + { + FacebookErrorMessage error = new FacebookErrorMessage(); + error.message = "Message describing the error"; + error.type = "OAuthException"; + error.code = 190; + error.subCode = 460; + error.userTitle = "A title"; + error.userMessage = "A message"; + error.traceId = "EJplcsCHuLu"; + + assertEquals(example, mapper.writeValueAsString(error)); + } + + @Test + public void testDeserialize() throws IOException + { + FacebookErrorMessage error = + mapper.readValue(example, FacebookErrorMessage.class); + + assertEquals("Message describing the error", error.message); + assertEquals("OAuthException", error.type); + assertEquals(new Integer(190), error.code); + assertEquals(new Integer(460), error.subCode); + assertEquals("A title", error.userTitle); + assertEquals("A message", error.userMessage); + assertEquals("EJplcsCHuLu", error.traceId); + } +}