我对语义Web编程很陌生。我正在学习如何使用Jena API编写Java代码来查询SPARQL端点。
这是我的Java代码:
public class TestJena
{
public static void main (String[] args)
{
String queryString = "prefix owl: <http://www.w3.org/2002/07/owl#>" +
"select ?class where { " +
" ?class a owl:Class } ";
String endpoint = "http://localhost:8890/sparql";
Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
query.setOffset(1);
QueryExecution qe = QueryExecutionFactory.sparqlService(endpoint, query);
try
{
ResultSet resultSet = qe.execSelect();
StringBuffer results = new StringBuffer();
List<String> columnNames = resultSet.getResultVars();
while(resultSet.hasNext())
{
QuerySolution solution = resultSet.next();
for(String var : columnNames)
{
results.append(var + ":");
if (solution.get(var) == null)
results.append("{null}");
else if (solution.get(var).isLiteral())
results.append(solution.getLiteral(var).toString());
else
results.append(solution.getResource(var).getURI());
results.append('\n');
}
results.append("----------\n");
}
System.out.print("Results are : "+results.toString());
}
finally
{
qe.close();
}
}
}
当我运行查询时,我得到以下例外:
Exception in thread "main" HttpException: 500
at com.hp.hpl.jena.sparql.engine.http.HttpQuery.rewrap(HttpQuery.java:414)
at com.hp.hpl.jena.sparql.engine.http.HttpQuery.execGet(HttpQuery.java:358)
at com.hp.hpl.jena.sparql.engine.http.HttpQuery.exec(HttpQuery.java:295)
at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.execResultSetInner(QueryEngineHTTP.java:346)
at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.execSelect(QueryEngineHTTP.java:338)
at com.TestJena.example.TestJena.main(TestJena.java:31)
当我在端点http://localhost:8890/sparql
运行上述查询时,它运行正常。
谁能告诉我问题出在哪里?为什么会抛出 HttpException:500 ?
谢谢!
答案 0 :(得分:0)
端点返回HTTP错误代码500(内部服务器故障)。客户端代码看起来很好,可能运行旧版本的Jena,因为它通常会打印更多细节(如果远程端提供任何细节)。
当您使用可能是HTML表单的http://localhost:8890/sparql时,而不是SPARQL端点。它可能需要其他参数 - 请参阅该服务器的文档。