【发布时间】:2015-03-17 20:37:30
【问题描述】:
我对语义 Web 编程非常陌生。我正在学习如何编写 Java 代码以使用 Jena API 查询 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 ?
【问题讨论】: