DATAREST-253, DATAREST-254 - Fixed serialization handling of transient properties.

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.
This commit is contained in:
Oliver Gierke
2014-02-25 07:40:07 +01:00
parent e5c1ce51c1
commit f409106139
4 changed files with 17 additions and 10 deletions

View File

@@ -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<ExceptionMessage> handleNotReadable(HttpMessageNotReadableException e) {
return badRequest(e);

View File

@@ -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);

View File

@@ -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);
}
}

View File

@@ -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}}"));
}