How can I serve an RDF representation of a Java model via Spring MVC?
I have JSON and XML representations working using Spring's content negotiation mechanisms and would like to do the same for RDF.
How can I serve an RDF representation of a Java model via Spring MVC?
I have JSON and XML representations working using Spring's content negotiation mechanisms and would like to do the same for RDF.
In addition, you must add the
charset
parameter. StringHttpMessageConverter uses as default character encoding ISO-8859-1 unless you change it. When the output format has a fixed encoding different from ISO-8859-1 (e.g. UTF-8), it must be declared inproduces
. This behaviour is consistent with specs. For example, when the model contains non ASCII characters and it is encoded inturtle
, the specification says that the media type text/turtle must contain the parametercharset
with the valueUTF-8
. Otherwise, it is optional.Assuming that you are using annotation driven configuration for your MVC application, this actually could be quite simple. Using the W3C's Media Types Issues for Text RDF Formats as a guide for content type specification, it's quite easy to augment your existing solution. The real question is what is your desired serialization type when a request for RDF is made? If we are utilizing Jena as the underlying model technology, then supporting any of the standard serializations is trivial out of the box. Json is the only one that provided me with a little difficulty, but you have already solved that.
As you can see, the implementation of the serialization (using standard Jena, and no additional libraries) is actually quite easy to do! The problem is ultimately just matching the proper serialization to the provided content-type.
EDIT April 19th, 2014
Previous content types were from a document capturing discussions of the working group. The post has been edited to reflect the content types of the IANA Media Type registry supplemented with the RDF1.1 N-Triples and JSON-LD standards.
I'll preface this by mentioning that I don't know anything about Spring MVC. But, if you have a Java object you want to serialize as RDF, a library like Empire (I'm the author), might help you. You can annotate the object to specify what things should be turned into RDF and then use the
RdfGenerator
class to return back an RDF graph representing the object. You can then use something like the RIO RDF writer from the Sesame framework to serialize the graph as whatever RDF serialization is requested.I know that's not Spring MVC specific, but if you can get the OutputStream & the requested content-type, you should be able to make it work with Empire & Sesame's RIO.