spring-guides / tut-rest

Building REST services with Spring :: Learn how to easily build RESTful services with Spring

Home Page:https://spring.io/guides/tutorials/rest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

findOne in interface cannot be applied to given types

handeyeco opened this issue · comments

Hey there, loving the tutorial - thanks for putting it together!

When I ran the first maven install I got an error regarding this line in BookmarkRestController.java:

return this.bookmarkRepository.findOne(bookmarkId);

Here's the error:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /Users/is5960/Code/tutorials/demo/src/main/java/com/example/demo/BookmarkRestController.java:[55,39] method findOne in interface org.springframework.data.repository.query.QueryByExampleExecutor<T> cannot be applied to given types;
  required: org.springframework.data.domain.Example<S>
  found: java.lang.Long
  reason: cannot infer type-variable(s) S
    (argument mismatch; java.lang.Long cannot be converted to org.springframework.data.domain.Example<S>)
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.538 s
[INFO] Finished at: 2018-04-20T14:51:10-05:00
[INFO] Final Memory: 32M/383M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project demo: Compilation failure
[ERROR] /Users/is5960/Code/tutorials/demo/src/main/java/com/example/demo/BookmarkRestController.java:[55,39] method findOne in interface org.springframework.data.repository.query.QueryByExampleExecutor<T> cannot be applied to given types;
[ERROR]   required: org.springframework.data.domain.Example<S>
[ERROR]   found: java.lang.Long
[ERROR]   reason: cannot infer type-variable(s) S
[ERROR]     (argument mismatch; java.lang.Long cannot be converted to org.springframework.data.domain.Example<S>)
[ERROR] 
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

After digging in a little, I found this Stack Overflow question that implies maybe the tutorial should use getOne or findOneByEmail. When I change the line of code I can compile fine:

return this.bookmarkRepository.getOne(bookmarkId);

I'm a total Java and Spring newbie, so maybe I'm totally off-the-mark. Just thought I'd share this issue in case someone else runs into it.

Actually ended up changing:

return this.bookmarkRepository.findOne(bookmarkId);

to:

Optional<Bookmark> bookmark = this.bookmarkRepository.findById(bookmarkId);
if (bookmark.isPresent()) {
    return bookmark.get();
} else {
    return null;
}

The application compiles and runs as expected so far. Would love feedback on whether this is the way to go or not.

Had exactly the same problem. Also tried to replace findOne(bookmarkId) with getOne(bookmarkId). Ended up with the same solution as @matthewbryancurtis - using an Optional and returning null when there is no bookmark. Even though it doesn't seem right to return null when the method declares Bookmark as the return type.

But here is the funny thing. A slightly elder version of the same tutorial found here: https://github.com/joshlong/bookmarks/tree/tutorial does compile (in the end, if skipTests is set) and can be made to run as expected. Don't know why.

Tested and working straightaway on clean Ubuntu 17.10.
Apache Maven 3.5.0
OpenJDK 1.8.0_171
Maven defaults to Spring Boot v1.5.1.RELEASE

I will post further updates as i do some more testing, it seems like an incompatibility issue.

UPDATE 1:
Tested and also working straightaway on clean Ubuntu 16.04.
Apache Maven 3.3.9
OpenJDK 1.8.0_171
Maven defaults to Spring Boot v1.5.1.RELEASE

@RaMdsC
This might indeed be the case. Looking at the documentation for Spring v1.5.x the method findOne() is inherited from the CrudRepository interface and has the signature:

T findOne(ID id)

while in the current version of Spring this method is inherited from QueryByExampleExecutor and has the signature:

<S extends T> Optional<S> findOne(Example<S> example)

In the current implementation of CrudRepository this method does not exist.

Resolved via 7f3644e