英文:
java.util.NoSuchElementException: No value present exception thrown when tried to check whether the emailID is registered or not in POSTGRES database
问题
以下是翻译好的部分:
我对Spring Boot还不太了解,我尝试检查未注册的emailID,以确定在Postgres数据库中是否存在给定的email id。但不幸的是,出现了NoSuchElementException异常。我已经在这里附上了异常截图供您参考。您能帮我解决这个问题吗?
TipTopController.java
@RestController
@RequestMapping(path = "/user")
public class TipTopController {
@Autowired
private UserService userService;
@PostMapping(path = "/registration")
public ResponseEntity<UserRegistrationResponse> registration(@RequestBody UserRegistrationModel userRegistrationModel){
UserRegistrationResponse userRegistrationResponse = userService.doUserRegistration(userRegistrationModel);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("description", "用户创建成功");
return ResponseEntity.status(HttpStatus.OK)
.headers(httpHeaders)
.body(userRegistrationResponse);
}
}
UserService.java
@Service
public class UserService {
private static final Logger logger = LogManager.getLogger(UserService.class);
@Autowired
private UserRepository userRepository;
@Transactional
public UserRegistrationResponse doUserRegistration(UserRegistrationModel userRegistrationModel){
UserRegistrationResponse userRegistrationResponse = new UserRegistrationResponse();
try{
logger.info("进入try块");
Optional<UserInformationRepo> userInformationRepo=userRepository.findById(userRegistrationModel.getEmail());
logger.info("获取的值为 "+userInformationRepo.get().getEmail());
logger.info("用户名为 "+userInformationRepo.get().getFirstName()+" "+userInformationRepo.get().getLastName());
if(userInformationRepo.isPresent()){
userRegistrationResponse.setStatus(false);
userRegistrationResponse.setMessage("此email id { "+userRegistrationModel.getEmail()+" } 已被其他用户使用");
}
else{
UserInformationRepo userInformationRepoSave = new UserInformationRepo();
userInformationRepoSave.setEmail(userRegistrationModel.getEmail());
userInformationRepoSave.setFirstName(userRegistrationModel.getFirstName());
userInformationRepoSave.setEmail(userRegistrationModel.getLastName());
userInformationRepoSave.setEmail(userRegistrationModel.getPassword());
userRepository.save(userInformationRepoSave);
userRegistrationResponse.setStatus(true);
userRegistrationResponse.setMessage("使用您的 "+ userRegistrationModel.getEmail() + " 邮箱成功创建新用户账户");
}
}
catch (Exception e){
logger.error(e.getMessage()+"*****"+e);
System.out.println("发生异常");
}
return userRegistrationResponse;
}
}
UserInformationRepo.java
@Entity
@Table(name = "USER_INFORMATION")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class UserInformationRepo {
@Id
@Column(name = "EMAIL")
private String email;
@Column(name="FIRSTNAME")
private String firstName;
@Column(name="LASTNAME")
private String lastName;
@Column(name="PASSWORD")
private String password;
}
UserRepository.java
@Repository
public interface UserRepository extends JpaRepository<UserInformationRepo, String> {
}
这是我在尝试使用POSTMAN工具访问注册API时遇到的异常。
英文:
I am new to Spring boot, I have tried to check with unregistered emailID whether the given email id is available or not in the Postgres database. But unfortunately got this NoSuchElementException. I have attached an exception screenshot here for your reference. Can you guys help me to come out of this problem?.
TipTopController.java
@RestController
@RequestMapping(path = "/user")
public class TipTopController {
@Autowired
private UserService userService;
@PostMapping(path = "/registration")
public ResponseEntity<UserRegistrationResponse> registration(@RequestBody UserRegistrationModel userRegistrationModel){
UserRegistrationResponse userRegistrationResponse = userService.doUserRegistration(userRegistrationModel);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("description","User created successfully");
return ResponseEntity.status(HttpStatus.OK)
.headers(httpHeaders)
.body(userRegistrationResponse);
}
}
UserService.java
@Service
public class UserService {
private static final Logger logger = LogManager.getLogger(UserService.class);
@Autowired
private UserRepository userRepository;
@Transactional
public UserRegistrationResponse doUserRegistration(UserRegistrationModel userRegistrationModel){
UserRegistrationResponse userRegistrationResponse = new UserRegistrationResponse();
try{
logger.info("Entering into try block");
Optional<UserInformationRepo> userInformationRepo=userRepository.findById(userRegistrationModel.getEmail());
logger.info("Get value is "+userInformationRepo.get().getEmail());
logger.info("Username is "+userInformationRepo.get().getFirstName()+" "+userInformationRepo.get().getLastName());
if(userInformationRepo.isPresent()){
userRegistrationResponse.setStatus(false);
userRegistrationResponse.setMessage("This email id { "+userRegistrationModel.getEmail()+" } is taken by another user already");
}
else{
UserInformationRepo userInformationRepoSave = new UserInformationRepo();
userInformationRepoSave.setEmail(userRegistrationModel.getEmail());
userInformationRepoSave.setFirstName(userRegistrationModel.getFirstName());
userInformationRepoSave.setEmail(userRegistrationModel.getLastName());
userInformationRepoSave.setEmail(userRegistrationModel.getPassword());
userRepository.save(userInformationRepoSave);
userRegistrationResponse.setStatus(true);
userRegistrationResponse.setMessage("New user account created with your " + userRegistrationModel.getEmail() + " emailId");
}
}
catch (Exception e){
logger.error(e.getMessage()+"*****"+e);
System.out.println("Exception occurred");
}
return userRegistrationResponse;
}
}
UserInformationRepo.java
@Entity
@Table(name = "USER_INFORMATION")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class UserInformationRepo {
@Id
@Column(name = "EMAIL")
private String email;
@Column(name="FIRSTNAME")
private String firstName;
@Column(name="LASTNAME")
private String lastName;
@Column(name="PASSWORD")
private String password;
}
UserRepository.java
@Repository
public interface UserRepository extends JpaRepository<UserInformationRepo, String> {
}
This is the exception I got while tried to hit the registration API from POSTMAN tool
答案1
得分: 0
-
你确定
com.ajithan.tiptop.tiptopapplication.domain.UserInformationRepo
是一个正确的完全限定类名 吗? -
你确定实体
UserInformationRepo
有适用于以下情况的构造函数:
UserInformationRepo(UI.EMAIL, UI.FIRSTNAME, UI.LASTNAME, UI.PASSWORD)
- 你不应该使用原生查询来做这个。你可以使用以下方式的 JPA 查询:
@Transactional
@Query(value = "select new com.ajithan.tiptop.tiptopapplication.domain.UserInformationRepo(UI.email, UI.firstName, UI.lastName, UI.password) from UserInformationRepo UI where UI.email = :email")
UserInformationRepo checkUserExists(@Param("email") String email);
- 为什么你不能只是使用标准的方法呢?
Optional<UserInformationRepo> findById(String id);
你可以避免使用你的自定义方法 checkUserExists
和 saveNewUser
,并开始使用标准的 JpaRepository
方法 findById
和 save
,像这样:
@Transactional
public UserRegistrationResponse doUserRegistration(UserRegistrationModel userRegistrationModel) {
UserRegistrationResponse userRegistrationResponse = new UserRegistrationResponse(); // 这里之前漏掉了!!!
try {
logger.info("进入 try 块");
Optional<UserInformationRepo> userInformationRepo = userRepository.findById(userRegistrationModel.getEmail());
System.out.println(userInformationRepo.getEmail());
if (userInformationRepo.isPresent()) {
userRegistrationResponse.setStatus(false);
userRegistrationResponse.setMessage("这个邮箱地址 { " + userRegistrationModel.getEmail() + " } 已被其他用户占用");
} else {
UserInformationRepo userInfo = new UserInformationRepo();
userInfo.setEmail(userRegistrationModel.getEmail());
userInfo.setFirstName(userRegistrationModel.getFirstName());
userInfo.setLastName(userRegistrationModel.getLastName());
userInfo.setPassword(userRegistrationModel.getPassword());
userRepository.save(userInfo);
userRegistrationResponse.setStatus(true);
userRegistrationResponse.setMessage("使用你的 " + userRegistrationModel.getEmail() + " 邮箱创建了新用户账户");
}
} catch (Exception e) {
logger.error(e.getMessage() + "*****" + e);
System.out.println("发生异常");
}
return userRegistrationResponse;
}
英文:
-
Are you sure that
com.ajithan.tiptop.tiptopapplication.domain.UserInformationRepo
is a correct fully qualified class name? -
Are you sure that the entity
UserInformationRepo
has an appropriate constructor for this:
UserInformationRepo(UI.EMAIL,UI.FIRSTNAME,UI.LASTNAME,UI.PASSWORD)
- You should not use a native query for this. You can use JPA query in this way:
@Transactional
@Query(value = "select new com.ajithan.tiptop.tiptopapplication.domain.UserInformationRepo(UI.email,UI.firstName,UI.lastName,UI.password) from UserInformationRepo UI where UI.email = :email")
UserInformationRepo checkUserExists(@Param("email") String email);
- Why you can't just use the standard method?
Optional<UserInformationRepo> findById(String id);
You can avoid to use your custom methods checkUserExists
and saveNewUser
and start to use the standard JpaRepository
methods findById
and save
in this way:
@Transactional
public UserRegistrationResponse doUserRegistration(UserRegistrationModel userRegistrationModel)
{
UserRegistrationResponse userRegistrationResponse = new UserRegistrationResponse(); // this was missed !!!
try{
logger.info("Entering into try block");
Optional<UserInformationRepo> userInformationRepo = userRepository.findById(userRegistrationModel.getEmail());
System.out.println(userInformationRepo.getEmail());
if(userInformationRepo.isPresent()){
userRegistrationResponse.setStatus(false);
userRegistrationResponse.setMessage("This email id { "+userRegistrationModel.getEmail()+" } is taken by another user already");
}
else{
UserInformationRepo userInfo = new UserInformationRepo();
userInfo.setEmail(userRegistrationModel.getEmail());
userInfo.setFirstName(userRegistrationModel.getFirstName())
userInfo.setLastName(userRegistrationModel.getLastName());
userInfo.setPassword(userRegistrationModel.getPassword());
userRepository.save(userInfo);
userRegistrationResponse.setStatus(true);
userRegistrationResponse.setMessage("New user account created with your " + userRegistrationModel.getEmail() + " emailId");
}
}
catch (Exception e){
logger.error(e.getMessage()+"*****"+e);
System.out.println("Exception occurred");
}
return userRegistrationResponse;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论