【问题标题】:How to return List<String> or String in Spring Data rest如何在 Spring Data rest 中返回 List<String> 或 String
【发布时间】:2016-10-28 09:36:23
【问题描述】:

我正在为我的应用程序使用 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

【问题讨论】:

  • 您不能(直接)使用 SDR,因为它只能返回“TestTerm”类型的注册实体。您需要声明另一个使用此存储库的控制器。
  • 您不需要单独的实现。顺便问一下,你用的是 Spring Boot 吗?
  • 尝试@Query("select distinct tt.term from TestTerm tt") 或执行:@Query("select distinct tt from TestTerm tt") 并返回TestTerm 实体列表,然后将所有术语放入列表中

标签: java spring rest spring-mvc spring-data-rest


【解决方案1】:

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

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

你还需要添加@Repository注解

【讨论】:

  • 你改过接口名了吗?
  • 你现在遇到了什么异常?可以发一下吗?
  • 我的错误如下{ "cause": null, "message": "PersistentEntity 不能为 null!" }
  • 为什么更改接口名称会有任何不同?
  • 你还需要添加@Repository注解
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
相关资源
最近更新 更多