如何在Java中使用自定义值引发自定义异常?
大家好,我正在使用MySQL的Spring Boot。我试图查找信息时出现以下错误 javax.persistence.UnuniquereSultException:查询未返回 唯一结果:2 在我的Repository类中,我有以下代码 可选findByIdOrEmail(整数id,字符串电子邮件) 我认为错误是因为如何在Java中使用自定义值引发自定义异常?,java,spring-boot,exception,spring-data-jpa,operators,Java,Spring Boot,Exception,Spring Data Jpa,Operators,大家好,我正在使用MySQL的Spring Boot。我试图查找信息时出现以下错误 javax.persistence.UnuniquereSultException:查询未返回 唯一结果:2 在我的Repository类中,我有以下代码 可选findByIdOrEmail(整数id,字符串电子邮件) 我认为错误是因为findByIdOrEmail由于或操作符而获取多条记录 因此,我使用了一个列表来获取值,下面是我的代码,我的目标是抛出一个异常,该异常专门显示每个重复的值 List<Use
findByIdOrEmail
由于或
操作符而获取多条记录
因此,我使用了一个列表
来获取值,下面是我的代码,我的目标是抛出一个异常,该异常专门显示每个重复的值
List<User> userList = userRepo.findByIdOrEmail(user.getId(), user.getEmail());
// There will be maximum of 2 records fetched by id and email and I didn't
check if each result is the users record
if (!userList.isEmpty() && userList.size() > 1)
throw new CustomException("Duplicate Record Found" +
" id: " + user.getId() + " and email: " + user.getEmail());
else if (!userList.isEmpty())
throw new CustomException("Duplicate Record Found" +
(userList.get(0).getId().equals(user.getId()) ? "id: " + user.getId() : "email: " + user.getEmail()));
List userList=userRepo.findbyidormail(user.getId(),user.getEmail());
//通过id和电子邮件获取最多2条记录,但我没有
检查每个结果是否为用户记录
如果(!userList.isEmpty()&&userList.size()>1)
抛出新的CustomException(“找到重复记录”+
id:“+user.getId()+”和电子邮件:“+user.getEmail());
如果(!userList.isEmpty()),则为else
抛出新的CustomException(“找到重复记录”+
(userList.get(0.getId().equals(user.getId())?“id:”+user.getId():“email:”+user.getEmail());
所以我想知道这种方法是最好的还是有其他的最佳实践?因为用户应该能够更新他/她的记录,但检查与现有其他记录的重复项。由于它有时会给出一个值列表,所以我必须在循环中检查它们。这是我在上面的代码中没有做的事情。那么,有没有另一种最好的方法或简单的方法来避免循环和多个if条件呢?非常感谢您的回答。提前感谢。让我们创建一个定制的ResourceReadyExistsException类。它将扩展RuntimeException类,您可以向其中添加任意数量的参数。我一直保持这样的简洁
public class ResourceAlreadyExistsException extends RuntimeException {
public ResourceAlreadyExistsException(String property, String value) {
super(String.format(
"Resource with property %s and value %s already exists." +
"Make sure to insert a unique value for %s",
property, value, property));
}
}
每当我需要检查唯一资源时,我都可以告诉用户哪个特定属性具有导致错误的值。此外,我会通知用户必须采取什么措施来避免错误
比如说,我选择使用error***作为我的ResourceReadyExistsException。不过,我需要将此错误消息连接到ExceptionResponseHandler。额外的方法与我们通常为处理所有异常而创建的方法非常相似。事实上,对于所有异常,您都可以轻松地复制粘贴此方法。您所要做的就是将Exception类更改为Exception并更改HttpStatus。
@ExceptionHandler(ResourceAlreadyExistsException.class)
public final ResponseEntity<ExceptionResponse> handleResourceAlreadyExistsException(
ResourceAlreadyExistsException ex, WebRequest req) {
ExceptionResponse exceptionResponse = new ExceptionResponse(
new Date(),
ex.getMessage(),
req.getDescription(false)
);
return new ResponseEntity<>(exceptionResponse, HttpStatus.UNPROCESSABLE_ENTITY);
@ExceptionHandler(ResourceReadyExistsException.class)
公共最终响应私人手资源已存在性别歧视(
ResourceReadyExistsException ex,WebRequest req){
ExceptionResponse ExceptionResponse=新的ExceptionResponse(
新日期(),
例如,getMessage(),
请求getDescription(错误)
);
返回新的ResponseEntity(exceptionResponse,HttpStatus.UNPROCESSABLE_实体);
让我们制作一个定制的ResourceReadyExistsException类。它将扩展RuntimeException类,您可以向它添加任意多的参数。我一直保持这样的简洁
public class ResourceAlreadyExistsException extends RuntimeException {
public ResourceAlreadyExistsException(String property, String value) {
super(String.format(
"Resource with property %s and value %s already exists." +
"Make sure to insert a unique value for %s",
property, value, property));
}
}
每当我需要检查唯一的资源时,我可以告诉用户哪个特定属性具有导致错误的值。此外,我还通知用户必须采取什么措施来避免错误
比方说,我选择在ResourceReadyExistsException中使用错误***。不过,我需要将此错误消息连接到ExceptionResponseHandler。额外的方法与我们通常创建的用于处理所有异常的方法非常相似。事实上,您可以轻松地为所有异常复制粘贴此方法。您所要做的就是s将异常类更改为您的异常并更改HttpStatus。
@ExceptionHandler(ResourceAlreadyExistsException.class)
public final ResponseEntity<ExceptionResponse> handleResourceAlreadyExistsException(
ResourceAlreadyExistsException ex, WebRequest req) {
ExceptionResponse exceptionResponse = new ExceptionResponse(
new Date(),
ex.getMessage(),
req.getDescription(false)
);
return new ResponseEntity<>(exceptionResponse, HttpStatus.UNPROCESSABLE_ENTITY);
@ExceptionHandler(ResourceReadyExistsException.class)
公共最终响应私人手资源已存在性别歧视(
ResourceReadyExistsException ex,WebRequest req){
ExceptionResponse ExceptionResponse=新的ExceptionResponse(
新日期(),
例如,getMessage(),
请求getDescription(错误)
);
返回新的ResponseEntity(exceptionResponse,HttpStatus.UNPROCESSABLE_实体);
Hi,看看它是否有用是的。我使用了
@ContollerAdvice
。但我只需要上述代码片段的解决方案?你考虑过Java 8流API..过滤器..等吗?还有其他吗?Hi,看看它是否有用是的。我使用了@ContollerAdvice
。但我只需要一个解决方案关于上面的代码片段?你考虑过Java 8流API..过滤器..等等吗?还有其他吗?非常感谢你的回答和时间。非常感谢你的回答和时间。