Java 无法在 Spring Boot 中使用 findOne() 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36613270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:28:58  来源:igfitidea点击:

Can't use method findOne() in Spring boot

javaspring

提问by H?ng Híp

My project is about a User-Manager web.

我的项目是关于用户管理器网络的。

I'm a new in Spring and Java.

我是 Spring 和 Java 的新手。

Here is my code:In the UserController

这是我的代码:在 UserController 中

@RequestMapping(value="/users/{name}",method = RequestMethod.GET)
public User showUser(@PathVariable("name") String name){
    return userService.findUser(name);
}

In the UserService:

在用户服务中:

 public User findUser(String name){
    return userRepository.findOne(name);
}

And in the Postman when I go to the link:http://localhost:8080/users/hunghip4(hunghip4 is a user I created)

在 Postman 中,当我转到链接时:http://localhost:8080/users/hunghip4(hunghip4 是我创建的用户)

{
  "timestamp": 1460570912129,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "org.springframework.dao.InvalidDataAccessApiUsageException",
  "message": "Provided id of the wrong type for class     com.uet.dhqg.usermanage.model.User. Expected: class java.lang.Integer, got class java.lang.String; nested exception is java.lang.IllegalArgumentException: Provided id of the wrong type for class com.uet.dhqg.usermanage.model.User. Expected: class java.lang.Integer, got class java.lang.String",
  "path": "/users/hunghip4"
}

The User model:

用户模型:

@Entity
@Table(name="user")
public class User extends HypermediaLinks {
@Id
@Column(name="id")
private int id;
@Column(name="name")
private String name;
@Column(name="pass")
private String pass;

public int getId(){
    return id;
}

public String getName(){
    return name;
}

public void serName(String name){
    this.name = name;
}

public String getPass(){
    return pass;
}

public void serPass(String pass){
    this.pass = pass;
}

}

The UserRepository:

用户存储库:

@Repository
public interface UserRepository extends CrudRepository<User,String>{
}

采纳答案by M. Deinum

Your UserRepositoryis defined as CrudRepository<User,String>. Where Useris the type and Stringthe type of the id. However your Userclass has an id field of the type intNOT of type String.

UserRepository的定义为CrudRepository<User,String>. idUser的类型和String类型在哪里。但是,您的User班级有一个类型为intNOT的 id 字段type String

First fix your UserRepositoryto be a proper representation of your User.

首先将您修复UserRepository为您的User.

public interface UserRepository extends CrudRepository<User, Integer> {}

Next create a method to find your Userby name.

接下来创建一个方法来查找你User的名字。

public User findByName(String name);

And call this from your controller instead of findOne. The findOneis used to find entities based on ID not on any random field of your entity.

并从您的控制器而不是findOne. 本findOne是用来寻找基于ID不是你的实体的任何随机领域的实体。

回答by VHS

As the exception suggests, you are passing the user name to the findOne method. But as per the method signature in the Spring docs, the method findOne expects the ID which is an Integer as its argument. Pass the user ID instead of name.

正如异常所暗示的那样,您将用户名传递给 findOne 方法。但是根据 Spring 文档中的方法签名,方法 findOne 需要一个整数作为其参数的 ID。传递用户 ID 而不是名称。

回答by Hamid Samani

Change your repository definition to

将您的存储库定义更改为

public interface UserRepository extends CrudRepository<User,Integer>

Your User id data type is int, so your repository Id should be int too. with this replacement findOnemethod also accepts integer data type as Userid is.

您的用户 ID 数据类型是 int,因此您的存储库 ID 也应该是 int。使用这种替换findOne方法也接受整数数据类型,就像Userid 一样。

回答by jayD

Change the version of the above artifact to 1.4.0.RELEASE in pom file and do maven update project.

在pom文件中将上述artifact的版本改为1.4.0.RELEASE,做maven update工程。

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
 </parent>

回答by Raghavendra M

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

Update Pom.xml and import maven changes

更新 Pom.xml 并导入 maven 更改

回答by Alamin Programmer

//Create a BookMarkRepsoitory extending the JpaRepo import org.springframework.data.jpa.repository.JpaRepository; public interface BookMarkRepository extends JpaRepository { }

//创建一个BookMarkRepsoitory扩展JpaRepo import org.springframework.data.jpa.repository.JpaRepository; 公共接口 BookMarkRepository 扩展 JpaRepository { }

then in your class to access one book or any value use this method @Override public UserDetails loadBookBookname(String bookName) throws UsernameNotFoundException { Optional book= readerRepository.findById(bookName); if (book.isPresent()){ return book.get(); } else return null; }

然后在您的类中访问一本书或任何值使用此方法 @Override public UserDetails loadBookBookname(String bookName) throws UsernameNotFoundException { Optional book= readerRepository.findById(bookName); if (book.isPresent()){ return book.get(); 否则返回空值;}