From f409106139a4f7edba783015b6bb4068a8d0bc01 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 25 Feb 2014 07:40:07 +0100 Subject: [PATCH] DATAREST-253, DATAREST-254 - Fixed serialization handling of transient properties. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AssociationOmittingSerializerModifier not correctly handles transient properties, which means it's handling null PersistentProperties correctly. Also, AbstractRepositoryRestController.handleNotReadable(…) is not bound to HttpMessageNotWritableException anymore which caused the former errors to be masked by a Spring MVC exception claiming about the exception handler method not being invokable correctly. --- .../AbstractRepositoryRestController.java | 3 +-- .../json/PersistentEntityJackson2Module.java | 17 ++++++++++------- .../data/rest/webmvc/jpa/Order.java | 5 +++++ .../PersistentEntitySerializationTests.java | 2 +- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/AbstractRepositoryRestController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/AbstractRepositoryRestController.java index 422bf9890..6ded121f0 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/AbstractRepositoryRestController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/AbstractRepositoryRestController.java @@ -47,7 +47,6 @@ import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.HttpMessageNotReadableException; -import org.springframework.http.converter.HttpMessageNotWritableException; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -109,7 +108,7 @@ class AbstractRepositoryRestController implements MessageSourceAware, Initializi return notFound(); } - @ExceptionHandler({ HttpMessageNotReadableException.class, HttpMessageNotWritableException.class }) + @ExceptionHandler({ HttpMessageNotReadableException.class }) @ResponseBody public ResponseEntity handleNotReadable(HttpMessageNotReadableException e) { return badRequest(e); 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 7e83c76e5..b1843bbdc 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 @@ -247,14 +247,17 @@ public class PersistentEntityJackson2Module extends SimpleModule { PersistentProperty persistentProperty = entity.getPersistentProperty(writer.getName()); - // Skip exported associations - if (persistentProperty.isAssociation() && resourceMetadata.isExported(persistentProperty)) { - continue; - } + if (persistentProperty != null) { - // Skip ids unless explicitly configured to expose - if (persistentProperty.isIdProperty() && !configuration.isIdExposedFor(entity.getType())) { - continue; + // Skip exported associations + if (persistentProperty.isAssociation() && resourceMetadata.isExported(persistentProperty)) { + continue; + } + + // Skip ids unless explicitly configured to expose + if (persistentProperty.isIdProperty() && !configuration.isIdExposedFor(entity.getType())) { + continue; + } } result.add(writer); diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Order.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Order.java index cf9bbc1f6..00189122d 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Order.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Order.java @@ -15,6 +15,7 @@ */ package org.springframework.data.rest.webmvc.jpa; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; @@ -67,4 +68,8 @@ public class Order { public void add(LineItem item) { this.lineItems.add(item); } + + public BigDecimal getPrice() { + return new BigDecimal(2.50); + } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java index f4844cf23..66daba460 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntitySerializationTests.java @@ -211,7 +211,7 @@ public class PersistentEntitySerializationTests { Arrays.asList(orderResource), new PageMetadata(1, 0, 10)); assertThat(mapper.writeValueAsString(persistentEntityResource), - is("{\"_embedded\":{\"orders\":[{\"lineItems\":[{\"name\":\"first\"},{\"name\":\"second\"}]" + is("{\"_embedded\":{\"orders\":[{\"lineItems\":[{\"name\":\"first\"},{\"name\":\"second\"}],\"price\":2.5" + ",\"_links\":{\"creator\":{\"href\":\"http://localhost:8080/orders/1/creator\"}}}]},\"" + "page\":{\"size\":1,\"totalElements\":10,\"totalPages\":10,\"number\":0}}")); }