Serving RDF view of Java model using Spring MVC?

How can I serve as a representation of the RDF Java model through Spring MVC?

I have JSON and XML views that work using Spring content consistency mechanisms and would like to do the same for RDF.

+2
source share
3 answers

Assuming you are using an annotation configuration for your MVC application, this might be quite simple. Using W3C Media Types for RDF Text Formats as a Guide for Content Type Specification, it is quite simple to enlarge an existing solution. The real question is, what is your desired type of serialization when creating an RDF query? If we use Jena as the base model, then supporting any of the standard serialization is trivial out of the box. Json is the only thing that helped me with difficulty, but you have already decided this.

As you can see, the implementation of serialization (using standard Jena and without additional libraries) is actually quite simple! The problem ultimately simply matches the correct serialization with the type of content provided.

EDIT 19 2014

, . , IANA Media Type registry, RDF1.1 N-Triples JSON-LD.

@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();
       }
    }
}
+3

, , Spring MVC. Java, RDF, , Empire ( ), . , , RDF, RdfGenerator, RDF, . - RIO RDF- Sesame , RDF.

, Spring MVC , OutputStream , Empire Sesame RIO.

+1

, charset. StringHttpMessageConverter ISO-8859-1, . , ISO-8859-1 (, UTF-8), produces. . , ASCII turtle, , text/turtle charset UTF-8. .

  @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();
    }
  }
0

Source: https://habr.com/ru/post/1537676/


All Articles