【发布时间】:2022-08-04 15:01:05
【问题描述】:
我需要在 Jena 事务中修改 RDFConnection 对象的请求(例如:更新)的标头(特别是 Content-Type)。更准确地说,我需要添加以下 Header Content_Type
Content-Type = [application/sparql-query; charset=UTF-8]
在下面的代码中
try (RDFConnection conn = connectionFactory.create()) {
Txn.executeWrite(conn, () -> {
conn.update(updateRequest);// HERE we want to setup the Content-Type in the header
});
}catch (Exception e) {... }
我已将 connectionFactory 设置如下
public RDFConnection create() {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
HttpClient httpClient = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
.build();
return
RDFConnectionRemote.create()
.destination(endpointURL)
.queryEndpoint(\"query\")
.updateEndpoint(\"update\")
.httpClient(httpClient)
.parseCheckSPARQL(true)
.build();
}
因此,我仍然需要在请求标头 content-Type 中指定请求正文的字符集。我怎么能用 JENA 做到这一点?
注意:默认的 JENA RDFConnection 设置是不够的,因为远程端点仍然需要明确的字符集规范来解析我在 URI 中包含法语口音的 SPARQL 请求。
提前致谢
-
SPARQL 更新字符串是为 UTF-8 定义的(仅)。如果端点不遵守这一点,这是一个错误,尽管请确保 updateRequest 字符串是 UTF-8(例如,如果它是从 Windows 上的文件中读取的,则可能不是)。服务器端点在运行什么?
-
Apache Jena 已切换(在 4.3 版)使用 JDK java.net.http 代码 - 您的示例使用 Apache HttpComponents。
-
@Andys 感谢您的快速反馈。我正在使用免费版的 stardog Triplestore。
curl --location --request POST \'http://localhost:5820/datasetTest/query\'\\ --header \'Origin: https://stardog.studio/\'\\ --header \'Content type: application/sparql-query; charset=UTF-8\'\\ --data-raw \'select distinct * where { graph ?g { <https://myTest.com/ontologies#Régime_Alimentaire> ?x ?y }}\'此查询返回预期结果。但是,如果我删除 Content-Type 或 charset=UTF-8,查询将返回空。 -
@Andys,我刚刚在 Stardog 社区中发现了一份报告,其中用户在使用 rdflib (python) 查询 Stardog 时必须将 Header Content-Type 指定为 charset=UTF-8 (https://community.stardog) .com/t/python3-8-rdflib-utf-8-problems-sparqlstore/3394) 由于此错误似乎在某些 Triplestore 中反复出现,有没有办法访问 Jena RDFConnection 中的 Content-Type 参数?谢谢 :)
-
curl 请求的标头错误:\"Content type:\"(没有 \"-\"),它是查询而不是更新。抱歉——这些细节很重要。如果根本没有 Content-type,那么它根本就不是 SPARQL 查询。如果查询不匹配,那么关于更新是否插入错误或查询没有正确传输,或者如果没有强制字符集,stardog 可能会尝试自动检测。正如耶拿票上所问的那样,您使用的是什么操作系统?
标签: java sparql jena triplestore