LiteralImpl 无法转换为 class Resource(LiteralImpl 和 Resource 在加载程序 'app' 的未命名模块中(Jena)

LiteralImpl cannot be cast to class Resource (LiteralImpl and Resource are in unnamed module of loader 'app' (Jena)

我正在创建一个 Web 应用程序 (spring + React),它将使用特定查询查询端点(select 由用户编辑)。出于此应用程序的目的,我正在使用 jena。

这是我使用jena的方法:

@Override
public List<String> save(QueryDto queryDto) {
    Endpoint endpoint = endpointRepository.findById(queryDto.getEndpointId()).orElseThrow(() -> new EndpointNotFoundException(queryDto.getEndpointId()));
    List<String> list = new ArrayList();

  //  String prefix = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>";
    Query query = new Query(queryDto.getName(),
            queryDto.getContent(),
            endpoint);

    RDFConnection conn = RDFConnectionFactory.connect(endpoint.getUrl());

    String subjectString = queryDto.getContent().split("\?")[1].split(" ")[0];
    System.out.println("SUBJECT STRING" + subjectString);

    QueryExecution qExec = conn.query(queryDto.getContent()) ; //SELECT DISTINCT ?s where { [] a ?s } LIMIT 100

    ResultSet rs = qExec.execSelect() ;
    /*qExec.close() ;
    conn.close() ;*/
    while (rs.hasNext()) {

        //System.out.println(results.getResourceModel());
        //ResultSetFormatter.out(System.out,results, q);
        QuerySolution qs = rs.next();
        System.out.println("qs: "+qs);

        Resource subject = qs.getResource(subjectString) ;

        System.out.println("Subject: "+subject.toString()) ;
        list.add(subject.toString());
    }


    queryRepository.save(query);
    return list;
}

我的应用适用于以下查询:SELECT DISTINCT ?s where { [] a ?s } LIMIT 100 selected 端点是:https://dbpedia.org/

但是当我尝试查询更复杂的查询时它不起作用:

前缀 dbr:http://dbpedia.org/resource/# 前缀 dbo:http://dbpedia.org/ontology/# select distinct ?birthName where {dbr:Woody_Allen dbo:birthName ?birthName} LIMIT 100 (selected 端点再次成为 https://dbpedia.org/

我得到以下异常:

SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: class org.apache.jena.rdf.model.impl.LiteralImpl cannot be cast to class org.apache.jena.rdf.model.Resource (org.apache.jena.rdf.model.impl.LiteralImpl and org.apache.jena.rdf.model.Resource are in unnamed module of loader 'app')] with root cause java.lang.ClassCastException: class org.apache.jena.rdf.model.impl.LiteralImpl cannot be cast to class org.apache.jena.rdf.model.Resource (org.apache.jena.rdf.model.impl.LiteralImpl and org.apache.jena.rdf.model.Resource are in unnamed module of loader 'app') at org.apache.jena.sparql.core.QuerySolutionBase.getResource(QuerySolutionBase.java:38) at com.finki.sparql_tool_web_app.service.impl.QueryServiceImpl.save(QueryServiceImpl.java:86) at com.finki.sparql_tool_web_app.web.QueryController.save(QueryController.java:34) at jdk.internal.reflect.GeneratedMethodAccessor66.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) at javax.servlet.http.HttpServlet.service(HttpServlet.java:681) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:889) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:833)

我从来没有遇到过类似的异常,而且我在网上也找不到太多相关信息。知道如何解决这个问题吗?

如果需要对我的代码(和我的应用程序)做一些解释:

编辑:我也尝试了以下查询,该应用程序对它们运行良好:

前缀 dbr:http://dbpedia.org/resource/ SELECT DISTINCT ?s where { [] a ?s } LIMIT 100

前缀 dbr:http://dbpedia.org/resource/ SELECT DISTINCT ?s where { dbr:Woody_Allen a ?s } LIMIT 100

只有当我添加“dbo:birthName”而不是“a”时才会抛出异常。

Resource subject = qs.getResource(subjectString)

资源是一个 URI 或一个空白节点,而不是文字。查询结果包括文字(例如字符串)。

Class RDFNode 是 Jena class 所有可能出现的 RDF 术语。

操作QuerySolution.get(varname)检索RDFNode。然后 cdoe 可以检查 RDF 术语的种类

例如

         RDFNode rn = qs.get(subjectString) ;
         if (rn.isLiteral() ) { ... }
         else if (rn.isURIResource() ) { ... }
         else // other cases blank node would presumable be an error