英文:
Registration form if email address already exist then throw an exception using Springboot and Jpa
问题
Controller类:
@RestController
public class RegistrationController {
@Autowired
private RegistrationService service;
@PostMapping("/registeruser")
public User registerUser(@RequestBody User user) throws Exception {
String tempEmailId = user.getEmailId();
if (tempEmailId != null && !"".equals(tempEmailId)) {
User userObject = service.fetchUserByEmailId(tempEmailId);
if (userObject != null) {
throw new Exception("User with " + tempEmailId + " already exists");
}
}
User userObject = null;
userObject = service.saveUser(user);
return userObject;
}
}
Repository:
public interface RegistrationRepository extends JpaRepository<User, Integer> {
public User findByEmailId(String emailId);
}
Service:
@Service
public class RegistrationService {
@Autowired
private RegistrationRepository repo;
public User saveUser(User user) {
return repo.save(user);
}
public User fetchUserByEmailId(String email) {
return repo.findByEmailId(email);
}
}
JSON响应:
{
"timestamp": "2020-08-26T06:28:01.369+00:00",
"status": 500,
"error": "Internal Server Error",
"message": "",
"path": "/registeruser"
}
英文:
I want to create a form that I can submit it and data store in h2. If email address is already in h2 then throw an exception. I have four packages(Controller, Model, Service, Repository). I dont get my exception message when I use an email that already exist in h2. Can you please help me where is the issue?
Controller class:
@RestController
public class RegistrationController {
@Autowired
private RegistrationService service;
@PostMapping("/registeruser")
public User registerUser(@RequestBody User user) throws Exception {
String tempEmailId = user.getEmailId();
if(tempEmailId !=null && !"".equals(tempEmailId)) {
User userObject = service.fetchUserByEmailId(tempEmailId);
if(userObject!=null) {
throw new Exception("User with "+tempEmailId+" is already exist");
}
}
User userObject = null;
userObject = service.saveUser(user);
return userObject;
}
Repository:
public interface RegistrationRepository extends JpaRepository<User, Integer> {
public User findByEmailId(String emailId);
}
Service:
@Service
public class RegistrationService {
@Autowired
private RegistrationRepository repo;
public User saveUser(User user) {
return repo.save(user);
}
public User fetchUserByEmailId(String email) {
return repo.findByEmailId(email);
}
}
Here is JSON response so I want my message printed but somehow not happening:
{
"timestamp": "2020-08-26T06:28:01.369+00:00",
"status": 500,
"error": "Internal Server Error",
"message": "",
"path": "/registeruser"
}
答案1
得分: 0
同样类型的问题我也遇到了。我所做的是,与其在 if 条件内部检查对象,你最好尝试检查对象的属性。
例如:
if (userObject.getEmailId().isEmpty()) {
throw new Exception("已存在邮箱为 " + tempEmailId + " 的用户");
}
这对我起作用了。
英文:
Same kind of problem i got. what i did is rather than checking the object inside the if condition you better try to check with attribute of the object.
eg
if(userObject.getEmailId().isEmpty()) {
throw new Exception("User with "+tempEmailId+" is already exist");
}
this worked for me.
答案2
得分: 0
你可以配置一个Spring的ExceptionHandler来自定义带有异常消息的响应体:
@ControllerAdvice
public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
@Autowired
public GlobalExceptionMapper() {
}
@ExceptionHandler(value = {Exception.class})
protected ResponseEntity<Object> handleBusinessException(Exception ex, WebRequest request) {
return handleExceptionInternal(ex, ex.getMessage(), new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
}
}
英文:
You can configure a Spring ExceptionHandler to customize your response body with exception messages:
@ControllerAdvice
public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
@Autowired
public GlobalExceptionMapper() {
}
@ExceptionHandler(value = {Exception.class})
protected ResponseEntity<Object> handleBusinessException(Exception ex, WebRequest request) {
return handleExceptionInternal(ex, ex.getMessage(), new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
}
答案3
得分: 0
如果您正在使用Spring Boot版本2.3或更高版本,则必须在application.properties
文件中将属性server.error.include-message
设置为always
。
英文:
If you are using Spring Boot version 2.3 or higher, the property server.error.include-message
must be set to always
at application.properties
file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论