From cf41d4b692bb3bf6e17db2dd7f5e39b721f7d4d2 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 14 Feb 2014 17:15:57 +0100 Subject: [PATCH] DATAREST-117 - Polishing. Simplified annotation detection. Added ignore guards to the association handling as well, so that associations not pointing to a repository managed resource can be ignored from being rendered as well. Original pull request: #135. --- .../json/PersistentEntityJackson2Module.java | 39 +++++++------------ .../data/rest/webmvc/jpa/Person.java | 28 +++++++++---- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java index 48ecd921c..f1b93ae4c 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java @@ -16,9 +16,10 @@ package org.springframework.data.rest.webmvc.json; import java.io.IOException; -import java.lang.annotation.Annotation; -import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -135,17 +136,13 @@ public class PersistentEntityJackson2Module extends SimpleModule { links.addAll(resource.getLinks()); final Map model = new LinkedHashMap(); + final Collection fieldsToIgnore = new HashSet(); - String[] fieldsToIgnore = new String[]{}; + JsonIgnoreProperties ann = entity.getType().getAnnotation(JsonIgnoreProperties.class); - for (Annotation annotation : entity.getType().getAnnotations()) { - if (annotation.annotationType().equals(JsonIgnoreProperties.class)) { - JsonIgnoreProperties ann = (JsonIgnoreProperties)annotation; - fieldsToIgnore = ann.value(); - break; - } + if (ann != null) { + fieldsToIgnore.addAll(Arrays.asList(ann.value())); } - final String[] finalFieldsToIgnore = fieldsToIgnore; try { @@ -165,22 +162,12 @@ public class PersistentEntityJackson2Module extends SimpleModule { return; } - final Method getter = property.getGetter(); - if (getter != null) { - final Annotation[] annotations = getter.getAnnotations(); - for (Annotation annotation : annotations) { - if (annotation.annotationType().equals(JsonIgnore.class)) { - if (((JsonIgnore)annotation).value()) { - return; - } - } - } + if (property.isAnnotationPresent(JsonIgnore.class)) { + return; } - for (String fieldToIgnore : finalFieldsToIgnore) { - if (property.getName().equals(fieldToIgnore)) { - return; - } + if (fieldsToIgnore.contains(property.getName())) { + return; } // Property is a normal or non-managed property. @@ -204,6 +191,10 @@ public class PersistentEntityJackson2Module extends SimpleModule { return; } + if (property.isAnnotationPresent(JsonIgnore.class)) { + return; + } + // Association Link was not added, probably because this isn't a managed type. Add value of property inline. if (metadata.isExported(property)) { model.put(property.getName(), wrapper.getProperty(property)); diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Person.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Person.java index 657624e58..48967c4a4 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Person.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Person.java @@ -1,3 +1,18 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.data.rest.webmvc.jpa; import java.util.ArrayList; @@ -14,19 +29,18 @@ import javax.persistence.ManyToOne; import javax.persistence.PrePersist; import javax.validation.constraints.NotNull; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonIgnoreType; import org.springframework.data.rest.core.annotation.Description; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; /** * An entity that represents a person. - * + * * @author Jon Brisbin */ @Entity -@JsonIgnoreProperties({"height", "weight"}) +@JsonIgnoreProperties({ "height", "weight" }) public class Person { private Long id; @@ -35,9 +49,7 @@ public class Person { @Description("A person's siblings") private List siblings = Collections.emptyList(); private Person father; @Description("Timestamp this person object was created") private Date created; - private int age; - private int height; - private int weight; + private int age, height, weight; public Person() {} @@ -134,4 +146,4 @@ public class Person { public void setWeight(int weight) { this.weight = weight; } -} \ No newline at end of file +}