@@ -136,23 +136,12 @@ public boolean hasFindOneMethod() {
136136 * @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeFindOne(java.io.Serializable)
137137 */
138138 @ Override
139- @ SuppressWarnings ("unchecked" )
140139 public <T > Optional <T > invokeFindOne (Serializable id ) {
141140
142141 Method method = methods .getFindOneMethod ()//
143142 .orElseThrow (() -> new IllegalStateException ("Repository doesn't have a find-one-method declared!" ));
144143
145- Object invoke = invoke (method , convertId (id ));
146-
147- if (Optional .class .isInstance (invoke )) {
148- return (Optional <T >) invoke ;
149- }
150-
151- if (invoke == null ) {
152- return Optional .empty ();
153- }
154-
155- return conversionService .convert (QueryExecutionConverters .unwrap (invoke ), Optional .class );
144+ return returnAsOptional (invoke (method , convertId (id )));
156145 }
157146
158147 /*
@@ -190,8 +179,8 @@ public void invokeDelete(Serializable id) {
190179 * @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)
191180 */
192181 @ Override
193- public Object invokeQueryMethod (Method method , MultiValueMap <String , ? extends Object > parameters , Pageable pageable ,
194- Sort sort ) {
182+ public Optional < Object > invokeQueryMethod (Method method , MultiValueMap <String , ? extends Object > parameters ,
183+ Pageable pageable , Sort sort ) {
195184
196185 Assert .notNull (method , "Method must not be null!" );
197186 Assert .notNull (parameters , "Parameters must not be null!" );
@@ -200,7 +189,7 @@ public Object invokeQueryMethod(Method method, MultiValueMap<String, ? extends O
200189
201190 ReflectionUtils .makeAccessible (method );
202191
203- return invoke (method , prepareParameters (method , parameters , pageable , sort ));
192+ return returnAsOptional ( invoke (method , prepareParameters (method , parameters , pageable , sort ) ));
204193 }
205194
206195 private Object [] prepareParameters (Method method , MultiValueMap <String , ? extends Object > rawParameters ,
@@ -262,6 +251,20 @@ private <T> T invoke(Method method, Object... arguments) {
262251 return (T ) ReflectionUtils .invokeMethod (method , repository , arguments );
263252 }
264253
254+ @ SuppressWarnings ("unchecked" )
255+ private <T > Optional <T > returnAsOptional (Object source ) {
256+
257+ if (Optional .class .isInstance (source )) {
258+ return (Optional <T >) source ;
259+ }
260+
261+ if (source == null ) {
262+ return Optional .empty ();
263+ }
264+
265+ return conversionService .convert (QueryExecutionConverters .unwrap (source ), Optional .class );
266+ }
267+
265268 /**
266269 * Converts the given id into the id type of the backing repository.
267270 *
0 commit comments