gpt4 book ai didi

java - 如何使spring数据jpa生成的方法在空参数上抛出异常

转载 作者:行者123 更新时间:2023-12-04 08:47:33 26 4
gpt4 key购买 nike

我有这个服务方法,它调用一个由 spring 生成的 repo 方法:
服务方式:

 public User findUserByEmail(String email) {
try {
Assert.notNull(email,"email is null!");
Optional<User> userOptional = userRepo.findUserDAOByEmail(email);
return userOptional.orElseThrow();
}
catch (...){
...
}
}
repo :
public interface UserRepo extends JpaRepository<User, Long> {
Optional<User> findUserByEmail(String email);
}
我想让 repo 方法抛出 IllegalArgumentException作为它的 friend (使用 Jparepository 开箱即用的方法),但它接受空参数“email”并在数据库中搜索它并返回不存在这样的用户的结果(ps:email 是在我的架构中被限制为不为空)
所以我做了 Assert.notNull(email,"email is null!");但我想知道是否有更好的方法来验证“电子邮件”不为空?

最佳答案

也可以像这样对参数施加约束 -

@Validated
@Repository
public interface UserRepo extends JpaRepository<User, Long> {
Optional<User> findUserByEmail(@NotNull String email);
}
@验证 使 Spring 能够调用各种验证约束。
工作示例

gradle 依赖
    compile 'org.springframework.boot:spring-boot-starter'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-validation'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile("mysql:mysql-connector-java:8.0.16")
用户存储库.java
@Validated
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
Optional<User> findUserByName(@NotEmpty String name);
}
控制台输出
javax.validation.ConstraintViolationException: findUserByName.name: must not be empty
at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:117) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at com.sun.proxy.$Proxy88.findUserByName(Unknown Source) ~[na:na]
at com.test.validation.controller.UserController.get(UserController.java:18) ~[main/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]

关于java - 如何使spring数据jpa生成的方法在空参数上抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64234362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com