我正在用java尝试一些新的设计模式,但是我弄不明白为什么我的模式不起作用。
我的目标是将一个用户帐户传递到一个数据传输对象中,然后通过检查新用户是否存在,以及是否使用getter和setter来创建一个用户,并用Map器将其保存到mongodb数据库中,从而使用该对象注册一个新用户。
一切似乎都很顺利,直到我在我的服务实现和Map器中得到一个未解析的setpassword方法调用,我不知道为什么。我通过扩展useraccount中的一个基本用户来获取setfirstname,这个用户帐户上有一个firstname字段。
任何帮助都会很好:)
package com.datingapp.model.user;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "userAccounts")
public class UserAccount extends User {
@Id
private String id;
private String email;
private String password;
private String lastname;
private String phoneNumber;
public UserAccount() {}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
package com.datingapp.dto.model.user;
import com.datingapp.model.user.User;
import com.datingapp.model.user.UserAccount;
public class UserAccountDto extends User {
private String email;
private String lastname;
private String password;
private String phoneNumber;
public UserAccountDto() {
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
package com.datingapp.dto.mapper;
import com.datingapp.dto.model.user.UserAccountDto;
import com.datingapp.model.user.UserAccount;
import org.springframework.stereotype.Component;
@Component
public class UserAccountMapper {
public static UserAccountDto toUserAccountDto(UserAccount userAccount) {
return new UserAccountDto()
.setEmail(userAccount.getEmail())
.setFirstname(userAccount.getFirstname())
.setLastname(userAccount.getLastname())
.setPassword(userAccount.getPassword())
.setPhoneNumber(userAccount.getPhoneNumber());
}
}
package com.datingapp.service.user;
import com.datingapp.dto.mapper.UserAccountMapper;
import com.datingapp.dto.model.user.UserAccountDto;
import com.datingapp.model.user.UserAccount;
import com.datingapp.repository.user.UserAccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
@Component
public class UserAccountImpl implements UserAccountService {
private UserAccountRepository userAccountRepository;
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Autowired
public UserAccountImpl(UserAccountRepository userAccountRepository, BCryptPasswordEncoder bCryptPasswordEncoder) {
this.userAccountRepository = userAccountRepository;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}
@Override
public UserAccountDto signup(UserAccountDto userDto) {
UserAccount user = userAccountRepository.findByEmail(userDto.getEmail());
if (user == null) {
user = new UserAccount()
.setEmail(userDto.getEmail())
.setPassword(bCryptPasswordEncoder.encode(userDto.getPassword()))
.setFirstName(userDto.getFirstname())
.setLastName(userDto.getFirstname())
.setMobileNumber(userDto.getPhoneNumber());
return UserAccountMapper.toUserAccountDto(userAccountRepository.save(user));
}
}
}
package com.datingapp.service.user;
import com.datingapp.dto.model.user.UserAccountDto;
import com.datingapp.model.user.UserAccount;
public interface UserAccountService {
UserAccountDto signup(UserAccountDto userAccountDto);
}
2条答案
按热度按时间l0oc07j21#
你的设置者是无效的方法,也就是说他们什么也不返回。
你可以做一个像这样的二传手来达到你的预期效果:
你必须加上
return this
在你的二传手上。0h4hbjxa2#
我知道毕达哥斯已经回答了你的问题,但让我给你一个提示。试着用Lombok山。lombok是一个java库,它可以帮助您避免样板代码。例如,使用
@Data
注解,你是在告诉Lombok山在引擎盖下创建所有的getter和setter。它会帮你很多。