Bonjour tout le monde.
Je suis en train de créer un projet Spring d’application Web d’E-Commerce sur Eclipse. Ce projet prévoit notamment un système d’inscription et de connexion
Pour ce projet Spring, j’ai crée un entity User, dont voici le code :
package entity;
import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id;
//import javax.persistence.Entity; //import javax.persistence.GeneratedValue; //import javax.persistence.GenerationType; //import javax.persistence.Id; //import javax.validation.constraints.Email; //import javax.validation.constraints.NotNull; //import javax.validation.constraints.Pattern;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString;
@Data @AllArgsConstructor @NoArgsConstructor @ToString @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer UserId; //@NotNull(message = "Please type your name") private String name; //@NotNull(message = "Please Enter your Mobile Number") //@Pattern(regexp = "[7896]{1}[0-9]{9}",message = "Input a valid mobile number") private String mobile; //@NotNull(message = "Please type your password") private String password; public Integer getUserId() { return UserId; } public void setUserId(Integer userId) { UserId = userId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }
}
J’ai crée un entity UserDTO dont voici le code :
package entity;
//import javax.validation.constraints.NotNull; //import javax.validation.constraints.Pattern;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString;
@Data @AllArgsConstructor @NoArgsConstructor @ToString public class UserDTO {
//@NotNull(message = "Enter Your Mobile Number")
//@Pattern(regexp = "[7896]{1}[0-9]{9}",message = "Input a valid mobile number")
private String mobileNumber;
//@NotNull(message = "Enter Your Password")
private String password;
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
J’ai crée un UserController dont voici le code :
package controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import javax.validation.Valid;
import entity.User; import entity.UserDTO; import service.UserLoginService; import service.UserService;
public class UserController { @Autowired private UserService uService;
@Autowired
private UserLoginService ulogService;
@PostMapping("/registration")
public ResponseEntity<User> saveUserController(@Valid @RequestBody User user) {
User responseUser = uService.saveUser(user);
return new ResponseEntity<User>(responseUser, HttpStatus.CREATED);
}
@PostMapping("/login")
public ResponseEntity<String> userLoginController(@Valid @RequestBody UserDTO userDto) {
String msg = ulogService.userLogin(userDto);
return new ResponseEntity<String>(msg,HttpStatus.ACCEPTED);
}
@PutMapping("/updateUser/{key}")
public ResponseEntity<User> updateUserCredentialController(@Valid @RequestBody User user, @PathVariable String key) {
User responseUser = uService.updateUserCredential(user, key);
return new ResponseEntity<User>(responseUser,HttpStatus.ACCEPTED);
}
@PostMapping("/logout/{key}")
public ResponseEntity<String> userLogoutController(@Valid @PathVariable String key) {
String msg = uService.userLogout(key);
return new ResponseEntity<String>(msg,HttpStatus.ACCEPTED);
}
}
J’ai crée un UserRepository dont voici le code :
package repository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository;
import entity.User;
@Repository public interface UserRepository extends JpaRepository<User, Integer>{
public User findByMobile(String mobile);
// public User findByUserId(Integer userId); }
J’ai crée un UserLoginService dont voici le code :
package service;
import entity.UserDTO;
public interface UserLoginService { public String userLogin(UserDTO userDto); }
Après avoir lancé mon application (via run Spring Boot App sur Eclipse), je vais sur mon navigateur affichant les champs username et password à remplir. Afin de faire un test, je mets 'user' pour username et 'user' pour password, et après avoir entré, la phrase 'Les identifications sont erronées’ s’affiche. Que dois-je faire pour rectifier cette erreur ?
[EDIT @viki53]: mise en forme des blocs de code