使用Spring MVC提供Java模型的RDF表示?

时间:2013-06-26 07:15:40

标签: java spring-mvc serialization rdf content-negotiation

如何通过Spring MVC提供Java模型的RDF表示?

我使用Spring的内容协商机制进行JSON和XML表示,并且希望对RDF执行相同的操作。

3 个答案:

答案 0 :(得分:3)

假设您正在为MVC应用程序使用注释驱动配置,这实际上可能非常简单。使用W3C的Media Types Issues for Text RDF Formats作为内容类型规范的指南,可以很容易地扩充现有的解决方案。真正的问题是,当提出RDF请求时,您想要的序列化类型是什么?如果我们使用Jena作为底层模型技术,那么支持任何标准序列化都是开箱即用的。 Json是唯一一个给我带来一点困难的人,但你已经解决了这个问题。

正如您所看到的,序列化的实现(使用标准的Jena,没有其他库)实际上很容易实现!问题最终只是将正确的序列化与提供的内容类型相匹配。

编辑2014年4月19日

以前的内容类型来自捕获工作组讨论的文档。该帖子已经过编辑,以反映IANA Media Type registry的内容类型,并辅以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();
       }
    }
}

答案 1 :(得分:1)

我将在前面提到我对Spring MVC一无所知。但是,如果你有一个Java对象想要序列化为RDF,像Empire这样的库(我是作者)可能对你有所帮助。您可以注释对象以指定应将哪些内容转换为RDF,然后使用RdfGenerator类返回表示对象的RDF图。然后,您可以使用Sesame框架中的RIO RDF编写器来序列化图形,就像请求任何RDF序列化一样。

我知道这不是Spring MVC特有的,但如果你能得到OutputStream&请求的内容类型,你应该能够使它与帝国和美国芝麻的里约。

答案 2 :(得分:0)

此外,您必须添加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();
    }
  }