diff --git a/src/main/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactory.java b/src/main/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactory.java index 64bf85db3..4b7f98552 100644 --- a/src/main/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactory.java +++ b/src/main/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactory.java @@ -42,6 +42,7 @@ import com.jayway.jsonpath.DocumentContext; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.Option; import com.jayway.jsonpath.ParseContext; +import com.jayway.jsonpath.PathNotFoundException; import com.jayway.jsonpath.TypeRef; import com.jayway.jsonpath.spi.mapper.MappingProvider; @@ -135,24 +136,31 @@ public class JsonProjectingMethodInterceptorFactory implements MethodInterceptor ResolvableType type = ResolvableType.forMethodReturnType(method); String jsonPath = getJsonPath(method); - if (returnType.getActualType().getType().isInterface()) { + try { - List result = context.read(jsonPath); - return result.isEmpty() ? null : result.get(0); - } + if (returnType.getActualType().getType().isInterface()) { - boolean isCollectionResult = Collection.class.isAssignableFrom(type.getRawClass()); - type = isCollectionResult ? type : ResolvableType.forClassWithGenerics(List.class, type); - type = isCollectionResult && JsonPath.isPathDefinite(jsonPath) - ? ResolvableType.forClassWithGenerics(List.class, type) : type; + List result = context.read(jsonPath); + return result.isEmpty() ? null : result.get(0); + } - List result = (List) context.read(jsonPath, new ResolvableTypeRef(type)); + boolean isCollectionResult = Collection.class.isAssignableFrom(type.getRawClass()); + type = isCollectionResult ? type : ResolvableType.forClassWithGenerics(List.class, type); + type = isCollectionResult && JsonPath.isPathDefinite(jsonPath) + ? ResolvableType.forClassWithGenerics(List.class, type) + : type; - if (isCollectionResult && JsonPath.isPathDefinite(jsonPath)) { - result = (List) result.get(0); - } + List result = (List) context.read(jsonPath, new ResolvableTypeRef(type)); + + if (isCollectionResult && JsonPath.isPathDefinite(jsonPath)) { + result = (List) result.get(0); + } - return isCollectionResult ? result : result.isEmpty() ? null : result.get(0); + return isCollectionResult ? result : result.isEmpty() ? null : result.get(0); + + } catch (PathNotFoundException o_O) { + return null; + } } /** diff --git a/src/test/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactoryUnitTests.java b/src/test/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactoryUnitTests.java index 7d1ba0861..e54b5e671 100644 --- a/src/test/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactoryUnitTests.java +++ b/src/test/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactoryUnitTests.java @@ -137,6 +137,11 @@ public class JsonProjectingMethodInterceptorFactoryUnitTests { assertThat(customer.getNestedCities(), hasSize(2)); } + @Test // DATACMNS-1144 + public void returnsNullForNonExistantValue() { + assertThat(customer.getName().getLastname(), is(nullValue())); + } + interface Customer { String getFirstname();