我在使用 Spring 2.7.4 时遇到此错误:没有找到类 com.library.project.library_el_aleph.model.Client 的主要或唯一构造函数我有两个构造函数用于我的 POJO 实体类中的两个可能的插入
@Repository
public interface RoleRespository extends CrudRepository<Role, Integer>{
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<Role> 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;
}
}
这是我的 RESTController 代码的一部分,使用设置 @RequesBody 输入的方法:
@RequestMapping(value= "/register", method = RequestMethod.POST)
public ResponseEntity<Object> 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<Role> 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<Object>(new Message("Ha ocurrido un error inesperado con el servidor, por favor intente nuevamente más tarde"), HttpStatus.BAD_REQUEST);
}
}
这是我第一次为同一个类使用多个构造函数,我认为这就是错误的原因......有人可以帮助我吗?
重写构造函数时,必须包含默认构造函数。
“我们还必须注意,当我们没有在类中使用任何构造函数时,java编译器会调用默认构造函数。但是,如果我们在类中使用了任何构造函数,则不会调用默认构造函数,无论是默认构造函数还是参数化构造函数。在这种情况下,Java 编译器会抛出一个异常,指出构造函数未定义。”
解决方案: 使用 Lombok @NoArgsConstructor 或
公共客户(){
}
我遇到了类似的问题,但还没找到解决方案