Question: 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.

Answer: 
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](http://www.w3.org/2008/01/rdf-media-types "W3C 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](http://www.iana.org/assignments/media-types/media-types.xhtml "Media Types") supplemented with the [RDF1.1 N-Triples](http://www.w3.org/TR/n-triples/ "RDF1.1 N-Triples") and [JSON-LD](http://www.w3.org/TR/json-ld/ "JSON-LD 1.0") standards.

```
@Controller
public class SpecialController 
{
    public Model model = null; // set externally

    @RequestMapping(value="your/service/location", method=RequestMethod.GET, produces={"application/x-javascript", "application/json", "application/ld+json"})
    public @ResponseBody String getModelAsJson() {
       // Your existing json response
    }

    @RequestMapping(value="your/service/location", method=RequestMethod.GET, produces={"application/xml", "application/rdf+xml"})
    public @ResponseBody String getModelAsXml() {
       // Note that we added "application/rdf+xml" as one of the supported types
       // for this method. Otherwise, we utilize your existing xml serialization
    }

    @RequestMapping(value="your/service/location", method=RequestMethod.GET, produces={"application/n-triples"})
    public @ResponseBody String getModelAsNTriples() {
       // New method to serialize to n-triple
       try( final ByteArrayOutputStream os = new ByteArrayOutputStream() ){
           model.write(os, "N-TRIPLE");
           return os.toString();
       }
    }

    @RequestMapping(value="your/service/location", method=RequestMethod.GET, produces={"text/turtle"})
    public @ResponseBody String getModelAsTurtle() {
       // New method to serialize to turtle
       try( final ByteArrayOutputStream os = new ByteArrayOutputStream() ){
           model.write(os, "TURTLE");
           return os.toString();
       }
    }

    @RequestMapping(value="your/service/location", method=RequestMethod.GET, produces={"text/n3"})
    public @ResponseBody String getModelAsN3() {
       // New method to serialize to N3
       try( final ByteArrayOutputStream os = new ByteArrayOutputStream() ){
           model.write(os, "N3");
           return os.toString();
       }
    }
}

```
In addition, you must add the `charset` parameter. [StringHttpMessageConverter](https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java) 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 in `produces`. This behaviour is consistent with specs. For example, when the model contains non ASCII characters and it is encoded in `turtle`, the specification says that the media type [text/turtle](http://www.w3.org/TR/turtle/#sec-mediaReg) must contain the parameter `charset` with the value `UTF-8`. Otherwise, it is optional. 

```
  @RequestMapping(value = "your/service/location", method = RequestMethod.GET, 
        produces = { "text/turtle;charset=UTF-8"})
  public @ResponseBody String getModelAsTurtle() throws IOException {
    try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        RDFDataMgr.write(os, model, RDFFormat.TURTLE_PRETTY);
        return os.toString();
    }
  }

```