[英]Caused by: java.lang.IllegalArgumentException: Not an entity:
[英]Caused by: java.lang.IllegalArgumentException: Entity must has id
搞不懂什么玄學。 此方法位於 class UserService 中:
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
User user = repository.getByEmail(email.toLowerCase());
if (user == null) {
throw new UsernameNotFoundException("User " + email + " isn't found");
}
1. AuthorizedUser authorizedUser = new AuthorizedUser(user);
2. Objects.requireNonNull(authorizedUser);
3. System.out.println(new AuthorizedUser(user) + " 1");
4. int userId = authorizedUser.getId();
5. return authorizedUser;
}
為什么go在第1-3行,第3行順利進入控制台:
UserTo{id=100000, name='VadimUserAdmin', email='vadim@gmail.com'} 1
在第 4 行,拋出異常:
Caused by: java.lang.IllegalArgumentException: Entity must has id
at org.springframework.util.Assert.notNull(Assert.java:201)
at topjava.quest.HasId.id(HasId.java:14)
at topjava.quest.AuthorizedUser.getId(AuthorizedUser.java:22)
at topjava.quest.service.UserService.loadUserByUsername(UserService.java:64)
我該怎么辦?
spring.security.version - 5.6.2
hibernate.version - 5.6.5.Final
class 授權用戶:
public class AuthorizedUser extends org.springframework.security.core.userdetails.User {
@Serial
private static final long serialVersionUID = 1L;
private UserTo userTo;
public AuthorizedUser(User user) {
super(user.getEmail(), user.getPassword(), true, true, true, true, user.getRoleSet());
setUserTo(Util.userAsTo(user));
}
public int getId() {
return userTo.id();
}
public void setUserTo(UserTo userTo) {
userTo.setPassword(null);
this.userTo = userTo;
}
@Override
public String toString() {
return userTo.toString();
}
}
class 用戶至:
public class UserTo extends BaseTo implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@NotBlank
@Size(min = 2, max = 100)
@ApiModelProperty(example = "New name")
private final String name;
@Email
@NotBlank
@Size(max = 100)
@ApiModelProperty(example = "newmame@gmail.com")
private final String email;
@NotBlank
@Size(min = 6, max = 32)
@ApiModelProperty(example = "newmame123")
private String password;
@ConstructorProperties({"id", "name", "email", "password"})
public UserTo(Integer id, String name, String email, String password) {
super(id);
this.name = name;
this.email = email;
this.password = password;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserTo{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
}
class 底座至:
public class BaseTo implements HasId {
@ApiModelProperty(hidden = true)
protected Integer id;
public BaseTo() {
}
public BaseTo(Integer id) {
this.id = id;
}
@Override
public Integer getId() {
return null;
}
@Override
public void setId(Integer id) {
this.id = id;
}
}
class HasId:
public interface HasId {
Integer getId();
void setId(Integer id);
default boolean isNew() {
return getId() == null;
}
default int id() {
Assert.notNull(getId(), "Entity must has id");
return getId();
}
}
我做錯了什么?
當您調用 HasId.id 時,您從 BaseTo.class 調用方法 getId(),而不是返回 null,更改方法 getId(){ return this.id;}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.