简体   繁体   English

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

[英]Can't use method findOne() in Spring boot

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>{
}

Your UserRepository is defined as CrudRepository<User,String> .您的UserRepository定义为CrudRepository<User,String> Where User is the type and String the type of the id.其中User是类型, String是 id 的类型。 However your User class has an id field of the type int NOT of type String .但是,您的User类有一个int类型的 id 字段,而不是String类型。

First fix your UserRepository to be a proper representation of your User .首先将您的UserRepository修复为您的User的正确表示。

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

Next create a method to find your User by name.接下来创建一个方法来按名称查找您的User

public User findByName(String name);

And call this from your controller instead of findOne .并从您的控制器而不是findOne调用它。 The findOne is used to find entities based on ID not on any random field of your entity. findOne用于根据 ID 而非实体的任何随机字段查找实体。

As the exception suggests, you are passing the user name to the findOne method.正如异常所暗示的那样,您将用户名传递给 findOne 方法。 But as per the method signature in the Spring docs, the method findOne expects the ID which is an Integer as its argument.但是根据 Spring 文档中的方法签名,方法 findOne 需要一个整数作为其参数的 ID。 Pass the user ID instead of name.传递用户 ID 而不是名称。

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.您的用户 ID 数据类型是 int,因此您的存储库 ID 也应该是 int。 with this replacement findOne method also accepts integer data type as User id is.使用此替换findOne方法也接受整数数据类型作为User ID。

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>
<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 更改

//Create a BookMarkRepsoitory extending the JpaRepo
```import org.springframework.data.jpa.repository.JpaRepository;
public interface BookMarkRepository
        extends JpaRepository<Book, String> {
}
```

then in your class to access one book or any value use this method
```
 @Override
                    public UserDetails loadBookBookname(String bookName)
                            throws UsernameNotFoundException {
                        Optional<Book> book= readerRepository.findById(bookName);
                        if (book.isPresent()){
                            return book.get();
                        }
                        else return null;
                    }```
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>

It is helpful很有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM