Im making a small application where i can save user details using spring-boot. i created the entities and their corresponding repositories. When ever i make a post request to add a user the id of the user object which is null at the point of saving to the data base.This id is auto generated(Auto Increment) in MySQL. From the POST request i get 3 fields which are username,email,password. The User class contains fields id,username,email,password. I've added the annotations
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
for the id field. an the constructors are
public User() { }
public User(String username, String email, String password) {
this.username = username;
this.email = email;
this.password = password;
}
my userService class
@Service
public class UserService implements UserServiceInterface {
@Autowired(required = true)
private UserRepository userrepository;
@Override
public User CreateNewUser(User user) {
return userrepository.save(user);
}
}
my userController class
@RestController
public class UserController {
UserService us = new UserService();
@RequestMapping(value ="/user",method = RequestMethod.POST)
public void RegisterUser(
@RequestParam(value="username") String username,
@RequestParam(value="email") String email,
@RequestParam(value="password") String password){
us.CreateNewUser(new User(username,email,password));
}
}
Any reason why i cant POST to save data to database? how to overcome this?