简体   繁体   English

使用Spring MVC服务Java模型的RDF表示形式?

[英]Serving RDF representation of Java model with Spring MVC?

How can I serve an RDF representation of a Java model via Spring MVC? 如何通过Spring MVC提供Java模型的RDF表示形式?

I have JSON and XML representations working using Spring's content negotiation mechanisms and would like to do the same for RDF. 我有使用Spring的内容协商机制工作的JSON和XML表示形式,并且希望对RDF进行相同的处理。

Assuming that you are using annotation driven configuration for your MVC application, this actually could be quite simple. 假设您正在为MVC应用程序使用注释驱动的配置,这实际上可能非常简单。 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. 使用W3C的文本RDF格式媒体类型问题作为内容类型规范的指南,可以很容易地扩展您现有的解决方案。 The real question is what is your desired serialization type when a request for RDF is made? 真正的问题是,当请求RDF时,您需要什么序列化类型? If we are utilizing Jena as the underlying model technology, then supporting any of the standard serializations is trivial out of the box. 如果我们将Jena用作基础模型技术,则支持任何标准序列化都是非常简单的。 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! 如您所见,序列化的实现(使用标准的Jena,没有其他库)实际上很容易做到! The problem is ultimately just matching the proper serialization to the provided content-type. 问题最终只是将适当的序列化与提供的内容类型匹配。

EDIT April 19th, 2014 编辑2014年4月19日

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. 该帖子经过编辑以反映IANA媒体类型注册表的内容类型,并以RDF1.1 N-TriplesJSON-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();
       }
    }
}

I'll preface this by mentioning that I don't know anything about Spring MVC. 首先,我将对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. 但是,如果您有一个Java对象要序列化为RDF,那么像Empire (我是作者)这样的库可能会为您提供帮助。 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. 您可以注释该对象以指定应将哪些内容转换为RDF,然后使用RdfGenerator类返回代表该对象的RDF图。 You can then use something like the RIO RDF writer from the Sesame framework to serialize the graph as whatever RDF serialization is requested. 然后,您可以使用Sesame框架中的RIO RDF编写器之类的东西根据请求的RDF序列化来序列化图形。

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. 我知道这不是特定于Spring MVC的,但是如果您可以获得OutputStream和请求的内容类型,则应该可以使其与Empire&Sesame的RIO一起使用。

In addition, you must add the charset parameter. 另外,您必须添加charset参数。 StringHttpMessageConverter uses as default character encoding ISO-8859-1 unless you change it. 除非您进行更改,否则StringHttpMessageConverter用作ISO-8859-1的默认字符编码。 When the output format has a fixed encoding different from ISO-8859-1 (eg UTF-8), it must be declared in produces . 当输出格式具有不同于ISO-8859-1的固定编码(例如UTF-8)时,必须在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 must contain the parameter charset with the value UTF-8 . 例如,当模型包含非ASCII字符并且以turtle编码时,规范说明介质类型text / turtle必须包含参数charset和值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();
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM