diff --git a/src/main/java/org/springframework/data/querydsl/QuerydslRepositoryInvokerAdapter.java b/src/main/java/org/springframework/data/querydsl/QuerydslRepositoryInvokerAdapter.java index 9ecc76a57..6281f1615 100644 --- a/src/main/java/org/springframework/data/querydsl/QuerydslRepositoryInvokerAdapter.java +++ b/src/main/java/org/springframework/data/querydsl/QuerydslRepositoryInvokerAdapter.java @@ -135,8 +135,8 @@ public class QuerydslRepositoryInvokerAdapter implements RepositoryInvoker { * @see org.springframework.data.repository.support.RepositoryInvoker#invokeQueryMethod(java.lang.reflect.Method, org.springframework.util.MultiValueMap, org.springframework.data.domain.Pageable, org.springframework.data.domain.Sort) */ @Override - public Object invokeQueryMethod(Method method, MultiValueMap parameters, Pageable pageable, - Sort sort) { + public Optional invokeQueryMethod(Method method, MultiValueMap parameters, + Pageable pageable, Sort sort) { return delegate.invokeQueryMethod(method, parameters, pageable, sort); } diff --git a/src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java b/src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java index 02fdf74f3..47081eafe 100644 --- a/src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java +++ b/src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java @@ -136,23 +136,12 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker { * @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeFindOne(java.io.Serializable) */ @Override - @SuppressWarnings("unchecked") public Optional invokeFindOne(Serializable id) { Method method = methods.getFindOneMethod()// .orElseThrow(() -> new IllegalStateException("Repository doesn't have a find-one-method declared!")); - Object invoke = invoke(method, convertId(id)); - - if (Optional.class.isInstance(invoke)) { - return (Optional) invoke; - } - - if (invoke == null) { - return Optional.empty(); - } - - return conversionService.convert(QueryExecutionConverters.unwrap(invoke), Optional.class); + return returnAsOptional(invoke(method, convertId(id))); } /* @@ -190,8 +179,8 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker { * @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeQueryMethod(java.lang.reflect.Method, java.util.Map, org.springframework.data.domain.Pageable, org.springframework.data.domain.Sort) */ @Override - public Object invokeQueryMethod(Method method, MultiValueMap parameters, Pageable pageable, - Sort sort) { + public Optional invokeQueryMethod(Method method, MultiValueMap parameters, + Pageable pageable, Sort sort) { Assert.notNull(method, "Method must not be null!"); Assert.notNull(parameters, "Parameters must not be null!"); @@ -200,7 +189,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker { ReflectionUtils.makeAccessible(method); - return invoke(method, prepareParameters(method, parameters, pageable, sort)); + return returnAsOptional(invoke(method, prepareParameters(method, parameters, pageable, sort))); } private Object[] prepareParameters(Method method, MultiValueMap rawParameters, @@ -262,6 +251,20 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker { return (T) ReflectionUtils.invokeMethod(method, repository, arguments); } + @SuppressWarnings("unchecked") + private Optional returnAsOptional(Object source) { + + if (Optional.class.isInstance(source)) { + return (Optional) source; + } + + if (source == null) { + return Optional.empty(); + } + + return conversionService.convert(QueryExecutionConverters.unwrap(source), Optional.class); + } + /** * Converts the given id into the id type of the backing repository. * diff --git a/src/main/java/org/springframework/data/repository/support/RepositoryInvoker.java b/src/main/java/org/springframework/data/repository/support/RepositoryInvoker.java index 18514209e..8a062a4b2 100644 --- a/src/main/java/org/springframework/data/repository/support/RepositoryInvoker.java +++ b/src/main/java/org/springframework/data/repository/support/RepositoryInvoker.java @@ -99,6 +99,6 @@ public interface RepositoryInvoker extends RepositoryInvocationInformation { * @return the result of the invoked query method. * @since 1.11 */ - Object invokeQueryMethod(Method method, MultiValueMap parameters, Pageable pageable, - Sort sort); + Optional invokeQueryMethod(Method method, MultiValueMap parameters, + Pageable pageable, Sort sort); }