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.
This commit is contained in:
@@ -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<String, Object> model = new LinkedHashMap<String, Object>();
|
||||
final Collection<String> fieldsToIgnore = new HashSet<String>();
|
||||
|
||||
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));
|
||||
|
||||
@@ -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<Person> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user