I am building a simple get rest call from MySQL database, the problem is that it returns an empty object.
The call itself is takes in an email (I know this is not the best approach), here is my code:
Entity:
@Entity
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private int id;
private String email;
private String password;
private String firstName;
private String userName;
private String lastName;
private boolean active;
@Temporal(TemporalType.DATE)
private Date createDate;
@Temporal(TemporalType.DATE)
private Date updateDate;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Collection<Role> roles;
// constructor
// get and setter
}
Repository:
public interface UserRepository extends JpaRepository<User, Long> {
// User findById (Integer Id);
@Query("SELECT u.id FROM User u where u.id = :id")
User findById(@Param("id") Integer id);
User findByEmail (String email);
}
Service:
@Service("userService")
public class UserService {
private String status, message;
private final HashMap map = new HashMap();
@Autowired
private UserRepository userRepository;
// @Autowired
// private RoleRepository roleRepository;
public User findByUserEmail (String email) {
return userRepository.findByEmail(email);
}
}
Controller:
@RestController("userControllerService")
@RequestMapping("/user/account")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/test-get/{email}")
public User jj(@PathVariable("email") String email){
return userService.findByUserEmail(email);
}
}
And my database happens to have the following data:
And here is the response I get after hitting the URL
I have no clue why my response is empty!