gpt4 book ai didi

java - 如何在 Spring Data rest 中返回 List 或 String

转载 作者:行者123 更新时间:2023-12-03 20:25:00 24 4
gpt4 key购买 nike

我正在为我的应用程序使用 Spring Data rest。

我在我的存储库中使用以下方法。

public interface TestRepository() extends BaseRepository<TestTerm, Integer>{
@Query("select distinct tt.term from TestTerm")
List<String> findDistinctTerm();
}

当我执行上述方法时。我收到以下错误

PersistentEntity must not be null

Spring Data Rest (SDR ) bug? Persistent Entity Must not be null 相关的问题

我该如何解决这个问题?

我必须为此编写单独的实现吗?如果是这样,有人为此提供示例吗?

更新:-

我试过这样

方法一:-

@Query("select DISTINCT term from ForbiddenTerm")
List<TestTerm> findDistinctByTerm();

错误:-

{ “原因”:空, "message": "PersistentEntity 不能为空!"

方法二:-

List<TestTerm> findDistinctByTerm();

错误:-

    ... 154 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.repository.support.Repositories]: Factory method 'repositories' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'forbiddenTermRepository': Invocation of init method failed; nested exception is java.util.NoSuchElementException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.3.RELEASE.jar!/:4.3.3.RELEASE]
... 175 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'forbiddenTermRepository': Invocation of init method failed; nested exception is java.util.NoSuchElementException

最佳答案

您需要像这样更改您的界面名称:

@Repository
public interface TestTermRepository() extends BaseRepository<TestTerm, Integer>{
@Query("select distinct tt.term from TestTerm")
List<String> findDistinctTerm();
}

还需要添加@Repository 注解

关于java - 如何在 Spring Data rest 中返回 List<String> 或 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40302245/

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