簡単なクエリを作成しようとしています。私はcustomizedPlaceRepositoryを実装するcustomizedPlaceRepositoryImplクラスを作成しました。問題は、ブラウザーでエンドポイントを呼び出そうとすると、「Java.lang.IllegalArgumentException:型クラスJava.lang.StringのPersistentEntityが見つかりませんでした!」 getResultList()を文字列にキャストするにはどうすればよいですか
customizedPlaceRepositoryImpl
public class customizedPlaceRepositoryImpl implements customizedPlaceRepository {
@PersistenceContext
private EntityManager entityManager;
@Override
public List<String> findAllDistinctPlaceName() {
String qlQuery = "SELECT name FROM place";
Query query = entityManager.createNativeQuery(qlQuery);
List<String> result = new ArrayList<>();
for(Object o: query.getResultList()){
result.add((String)o);
}
return result;
}
}
customizedPlaceRepository
public interface customizedPlaceRepository {
List<String> findAllDistinctPlaceName();
}
スタックトレース
Java.lang.IllegalArgumentException: Couldn't find PersistentEntity for type class Java.lang.String!
at org.springframework.data.mapping.context.PersistentEntities.lambda$getRequiredPersistentEntity$2(PersistentEntities.Java:96) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at Java.util.Optional.orElseThrow(Optional.Java:290) ~[na:1.8.0_201]
at org.springframework.data.mapping.context.PersistentEntities.getRequiredPersistentEntity(PersistentEntities.Java:95) ~[spring-data-commons-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.wrap(PersistentEntityResourceAssembler.Java:72) ~[spring-data-rest-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.Java:55) ~[spring-data-rest-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.entitiesToResources(AbstractRepositoryRestController.Java:110) ~[spring-data-rest-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.toResources(AbstractRepositoryRestController.Java:80) ~[spring-data-rest-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.data.rest.webmvc.RepositorySearchController.lambda$toResource$1(RepositorySearchController.Java:209) ~[spring-data-rest-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at Java.util.Optional.map(Optional.Java:215) ~[na:1.8.0_201]
at org.springframework.data.rest.webmvc.RepositorySearchController.toResource(RepositorySearchController.Java:206) ~[spring-data-rest-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.data.rest.webmvc.RepositorySearchController.executeSearch(RepositorySearchController.Java:190) ~[spring-data-rest-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:62) ~[na:1.8.0_201]
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43) ~[na:1.8.0_201]
at Java.lang.reflect.Method.invoke(Method.Java:498) ~[na:1.8.0_201]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.Java:189) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.Java:138) ~[spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.Java:102) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.Java:895) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.Java:800) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.Java:87) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.Java:1038) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.Java:942) ~[spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:1005) [spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.Java:897) [spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.Java:634) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.Java:882) [spring-webmvc-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.Java:741) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:231) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.Tomcat.websocket.server.WsFilter.doFilter(WsFilter.Java:53) [Tomcat-embed-websocket-9.0.14.jar:9.0.14]
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at com.whateat.reciper.security.SimpleCORSFilter.doFilter(SimpleCORSFilter.Java:35) [classes/:na]
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.Java:90) [spring-boot-actuator-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107) [spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.Java:117) [spring-boot-actuator-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.Java:106) [spring-boot-actuator-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107) [spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.Java:200) [spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.Java:107) [spring-web-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:193) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:166) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.Java:199) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.catalina.core.StandardContextValve.invoke(StandardContextValve.Java:96) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.Java:490) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.catalina.core.StandardHostValve.invoke(StandardHostValve.Java:139) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.Java:92) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.Java:74) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.Java:343) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.coyote.http11.Http11Processor.service(Http11Processor.Java:408) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.Java:66) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.Java:834) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.Tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.Java:1417) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at org.Apache.Tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.Java:49) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1149) [na:1.8.0_201]
at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:624) [na:1.8.0_201]
at org.Apache.Tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.Java:61) [Tomcat-embed-core-9.0.14.jar:9.0.14]
at Java.lang.Thread.run(Thread.Java:748) [na:1.8.0_201]
EndPoint http:// localhost:8085/v1/places/search/findAllDistinctPlaceName
クエリにSDRのデフォルトのエンドポイントを使用しようとしました。ただし、SDRは応答をResourceオブジェクトにラップするため、これらのエンドポイントは、persistentEntities(またはPersistentEntitiesのコレクション)のみを返すことができます。 (例:応答へのselfLinkの追加、ページネーションの処理など)
ただし、応答はリストであるため、ResourceAssemblerはそれを処理できません。
このエンドポイントを処理するために本当にSDRを使用したい場合は、ResourceAssemblerを何らかの方法で拡張できますが、それは悪い考えであり、おそらく努力する価値はないと思います。
代わりに、適切なメソッドを使用して一意のコントローラーを作成し、そこからリポジトリメソッドを呼び出して、応答のResourceオブジェクトを手動で作成します。