rdf4j
rdf4j copied to clipboard
Support application/sparql-results+json by default for TupleQueries
Problem description
I have a sparql endpoint which only accepts application/sparql-results+json requests.
So I added:
SPARQLRepository repo = new SPARQLRepository(sparqlEndpoint);
repo.setAdditionalHttpHeaders(Map.of("Accept", "application/sparql-results+json,*/*;q=0.9"));
This worked well, for the request. But then I got the error that application/sparql-results+json was not a supported content type for the response parser.
This sent me on a bit of a wild goose chase, where I ultimately discovered TupleQueryResultParserRegistry. I almost started writing my own parser, but then it turns out that SPARQLResultsJSONParserFactory just exists already, so all I had to do was add the following line:
TupleQueryResultParserRegistry.getInstance().add(new SPARQLResultsJSONParserFactory());
and everything worked beautifully.
That got me thinking, why not add this parser to the registry by default? It seems like a no-brainer. I looked around to see where that would be done, but couldn't find it due to the indirection of the ServiceLoader, I don't normally work in Java.
Preferred solution
It would be nice if TupleQueries supported json responses out of the box.
Are you interested in contributing a solution yourself?
Yes
Alternatives you've considered
As mentioned, the feature essentially already exists, it just needs an extra configuration.
Anything else?
No response
The parser/serializer relies on Java SPI. Adding the corresponding dependency in maven should automatically register as a service.
Hmm I was using the full rdf4j-storage bundle in my pom file, was there something else I should have added?
Try adding rdf4j-client as a second dependency.