我有一个服务方法,它调用一个由spring生成的repo方法:
服务方式:
public User findUserByEmail(String email) {
try {
Assert.notNull(email,"email is null!");
Optional<User> userOptional = userRepo.findUserDAOByEmail(email);
return userOptional.orElseThrow();
}
catch (...){
...
}
}
回购:
public interface UserRepo extends JpaRepository<User, Long> {
Optional<User> findUserByEmail(String email);
}
我希望repo方法抛出IllegalArgumentException
作为它的朋友(Jparepository
开箱即用的方法(,但它接受了null参数";电子邮件";并在数据库中搜索它,并返回不存在这样的用户的结果(附言:在我的模式中,电子邮件被限制为非空(
所以我做了Assert.notNull(email,"email is null!");
但我想知道是否有更好的方法来验证这一点;电子邮件";不是null?
也可以在参数上设置约束,如下所示-
@Validated
@Repository
public interface UserRepo extends JpaRepository<User, Long> {
Optional<User> findUserByEmail(@NotNull String email);
}
@Validated使Spring能够调用各种验证约束。
工作示例
渐变依赖性
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]
存储库无法直接从服务类中访问它,因此您需要创建一个用@service注释的服务类,然后使用UserRepo存储库接口实现您自己的方法来查找用户,然后您可以将服务类注入测试单元并进行自己的测试。