使用Spring MVC提供Java模型的RDF表示? [英] Serving RDF representation of Java model with Spring MVC?

查看:148
本文介绍了使用Spring MVC提供Java模型的RDF表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I serve an RDF representation of a Java model via Spring MVC?

我使用Spring的内容协商机制进行JSON和XML表示,并希望对RDF也这样做。

I have JSON and XML representations working using Spring's content negotiation mechanisms and would like to do the same for RDF.

推荐答案

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

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.

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

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.

编辑2014年4月19日

以前的内容类型来自捕获工作组讨论的文档。该帖子已经过编辑,以反映 IANA媒体类型注册表,并辅以 RDF1.1 N-Triples JSON-LD 标准。

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.

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

这篇关于使用Spring MVC提供Java模型的RDF表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆