rdf4j icon indicating copy to clipboard operation
rdf4j copied to clipboard

Support application/sparql-results+json by default for TupleQueries

Open abeforgit opened this issue 6 months ago • 3 comments

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

abeforgit avatar May 17 '25 00:05 abeforgit

The parser/serializer relies on Java SPI. Adding the corresponding dependency in maven should automatically register as a service.

hmottestad avatar May 18 '25 05:05 hmottestad

Hmm I was using the full rdf4j-storage bundle in my pom file, was there something else I should have added?

abeforgit avatar May 20 '25 17:05 abeforgit

Try adding rdf4j-client as a second dependency.

hmottestad avatar May 22 '25 07:05 hmottestad