Search code examples
javaspringhibernatespring-mvcannotations

How to see result of findByUsername delievered by CrudRepository


Need to see the search result of the:

@Repository
public interface UserRepository extends CrudRepository<User, Long> {    
    public User findByUsername(String username);        
}

which I call from my controller:

@RequestMapping(value = "/users/find", method = RequestMethod.GET)
public @ResponseBody User findByUsername(@RequestParam("username") String userName) {
    return usersRepo.findByUsername(userName);
}

The method above is called via:

@GET("/users/find")
public User findByUsername(String userName); 

And the class User is here:

@Entity
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;    

    private String username;
    etc...

Results of the default methods I can see in my browser by typing e.g. localhost:8080/users

I tried many possible requests but it seems that I'm missing something. Any help is appreciated.


Solution

  • Try this:

    Here you will get plane User object on browser so try to change return type to String.

    @RequestMapping(value = "/users/find", method = RequestMethod.GET)
    @ResponseBody
    public User findByUsername(@RequestParam("username") String username) {
        return usersRepo.findByUsername(username);
    }
    

    Hit this URL from local machine

    localhost:8080/users/find?username=99