Код: Выделить всё
@Repository
public interface RoleRespository extends CrudRepository{
Role findByUserName(RoleList roleName);
public Cliente(@NotEmpty @Pattern(regexp = "^[A-Z][a-zA-Z]+") String userName,
@NotEmpty String email,
@NotEmpty String contraseña) {
this.userName = userName;
this.email = email;
this.contraseña = contraseña;
}
public Cliente(String productosCliente,
@NotEmpty @Pattern(regexp = "^[A-Z][a-zA-Z]+") String userName, @NotEmpty String segundoNombre,
@NotEmpty String primerApellido, @NotEmpty String segundoApellido, @NotEmpty String ciudadCliente,
@NotEmpty String direccionCliente, @NotEmpty String email, @NotEmpty String contraseña,
@NotNull @Past Date fechaNacimiento, @NotNull Set roles) {
this.productosCliente = productosCliente;
this.userName = userName;
this.segundoNombre = segundoNombre;
this.primerApellido = primerApellido;
this.segundoApellido = segundoApellido;
this.ciudadCliente = ciudadCliente;
this.direccionCliente = direccionCliente;
this.email = email;
this.contraseña = contraseña;
this.fechaNacimiento = fechaNacimiento;
this.roles = roles;
}
}
Код: Выделить всё
@RequestMapping(value= "/register", method = RequestMethod.POST)
public ResponseEntity register(@Valid @RequestBody NewUser newUser,
BindingResult send){
if(send.hasErrors())
return new ResponseEntity(new Message("Error en el registro, por favor
valide nuevamente"), HttpStatus.BAD_REQUEST);
try {
Cliente creatingUser = new Cliente(newUser.getPrimerNombre(),
newUser.getEmail(), passwordEncoder.encode(newUser.getPassword()));
Set roles = new HashSet();
roles.add(roleServices.getRoleByName(RoleList.ROLE_USER).get());
if(newUser.getRoles().contains("Admin"))
roles.add(roleServices.getRoleByName(RoleList.ROLE_ADMIN).get());
creatingUser.setRoles(roles);
clienteServices.saveNewUser(creatingUser);
return new ResponseEntity(new Message("El usuario ha sido creado correctamente"), HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity(new Message("Ha ocurrido un error inesperado con el servidor, por favor intente nuevamente más tarde"), HttpStatus.BAD_REQUEST);
}
}
Подробнее здесь: https://stackoverflow.com/questions/742 ... pring-boot