java - Spring boot rest API - Querydsl,谓词不能为空

标签 java spring spring-boot

Predicate 是否应该使用 MongoRepository 可以为空?

标准 JPA PagingAndSortingRepository 似乎总是返回有效的 Predicate,无论提供了哪些查询参数。

堆栈跟踪

java.lang.IllegalArgumentException: Predicate must not be null!
        at org.springframework.util.Assert.notNull(Assert.java:198) ~[spring-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
        at org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor.findAll(QuerydslMongoPredicateExecutor.java:165) ~[spring-data-mongodb-2.1.3.RELEASE.jar:2.1.3.RELEASE]
        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.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359) ~[spring-data-commons-2.1.3.RELEASE.jar:2.1.3.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) ~[spring-data-commons-2.1.3.RELEASE.jar:2.1.3.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644) ~[spring-data-commons-2.1.3.RELEASE.jar:2.1.3.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608) ~[spring-data-commons-2.1.3.RELEASE.jar:2.1.3.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.3.RELEASE.jar:2.1.3.RELEASE]
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.3.RELEASE.jar:2.1.3.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
        at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.1.3.RELEASE.jar:2.1.3.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
        at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.1.3.RELEASE.jar:2.1.3.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.3.RELEASE.jar:5.1.3.RELEASE]

curl

http://localhost:8080/api/notifications?page=1 # stacktrace
http://localhost:8080/api/notifications?id=1&page=1" # does work

Controller

@RestController
@RequestMapping(produces = "application/json; charset=UTF-8")
@Api(description = "Operations concerning notifications")
public class NotificationController {

    @Autowired
    private NotificationFacade notificationFacade;

    @Autowired
    private NotificationRepository notificationRepository;

    @ApiOperation(value = "Retrieve a list of available notifications")
    @GetMapping("/api/notifications")
    public Page<Notification> findAll(@QuerydslPredicate() Predicate predicate,
                                      @PageableDefault(sort = "id") Pageable pageable) {
        return notificationRepository.findAll(predicate, pageable);
    }

}

存储库

public interface NotificationRepository extends MongoRepository<Notification, String>, QuerydslPredicateExecutor<Notification> {}

实体

@Data
@Document(collection = "notifications")
public class Notification {

    @Id
    private String id;

    @NonNull
    private String message;



}

最佳答案

添加额外的支票

        if (predicate == null) {
            predicate = QNotification.notification.id.ne("");
        }
        return notificationRepository.findAll(predicate, pageable);

关于java - Spring boot rest API - Querydsl,谓词不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54316142/

相关文章:

java - 自定义 future 对象

java - 屏幕旋转适配器notifyDataSetChanged 不起作用

java - 使用 Spring MVC 在应用程序启动时执行 Java 类

java - 将部分 JSON 返回到对象 Spring boot

java - 日志记录不适用于 Spring boot 2.1.1 和 JDK 11 升级

java - 为什么序列化代理模式与对象图包含循环的类不兼容

java - 当我为 JFrame 使用 FlowLayout 时,为什么我的图像没有出现?

java - Spring MongoDB - 为什么唯一索引也应用于嵌入文档?

java - Spring 支架 Controller

javascript - @SpringBootApplication 不加载静态文件