Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use method findOne() in Spring boot

Tags:

java

spring

My project is about a User-Manager web.

I'm a new in Spring and Java.

Here is my code:In the 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)

{
  "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>{
}
like image 952
MiH Avatar asked Apr 14 '16 03:04

MiH


People also ask

What is spring boot findOne?

The findOne is used to find entities based on ID not on any random field of your entity.

What is findOne in Java?

findOne() is defined as <S extends T> Optional<S> findOne(Example<S> example); . It means that in your case it accepts a Example<Reader> and returns an Optional<Reader> .

How does JPA findById work?

Its findById method retrieves an entity by its id. The return value is Optional<T> . Optional<T> is a container object which may or may not contain a non-null value. If a value is present, isPresent returns true and get returns the value.


1 Answers

Your UserRepository is defined as CrudRepository<User,String>. Where User is the type and String the type of the id. However your User class has an id field of the type int NOT of type String.

First fix your UserRepository to be a proper representation of your User.

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

Next create a method to find your User by name.

public User findByName(String name);

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

like image 158
M. Deinum Avatar answered Sep 27 '22 16:09

M. Deinum