J'essaie de faire une simple requête. J'ai créé une classe CustomPlaceRepositoryImpl qui implémente CustomPlaceRepository. Le problème est que lorsque j'essaie d'appeler le point de terminaison dans le navigateur, il me renvoie "Java.lang.IllegalArgumentException: impossible de trouver PersistentEntity pour la classe de type Java.lang.String!" Comment convertir le getResultList () en chaîne
customPlaceRepositoryImpl
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;
}
}
customPlaceRepository
public interface customizedPlaceRepository {
List<String> findAllDistinctPlaceName();
}
Trace de la pile
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
Vous essayez d'utiliser les points de terminaison par défaut de SDR pour votre requête. Mais ces points de terminaison ne peuvent renvoyer que des persistentEntities (ou une collection de PersistentEntities), car SDR encapsule la réponse dans un objet Resource. (par exemple, ajouter le selfLink à la réponse, gérer la pagination, etc.)
Mais votre réponse est une liste, donc le ResourceAssembler ne peut pas le traiter.
Si vous voulez vraiment utiliser SDR pour gérer ce point de terminaison, vous pouvez peut-être étendre le ResourceAssembler d'une manière ou d'une autre, mais je pense que c'est une mauvaise idée et ne vaut probablement pas la peine.
Au lieu de cela, créez un contrôleur unique avec la méthode appropriée, appelez la méthode de référentiel à partir de là et créez manuellement l'objet de ressource de réponse.