我应该做什么来序列化/编组(反之亦然)引用其他资源的资源?

发布于 2024-10-04 20:41:30 字数 384 浏览 2 评论 0原文

我有一个 SpringServlet(来自 Jersey),它现在以非常基本的方式公开我的 JPA 注释的 POJO。例如,我没有返回对象的实际表示,而是返回了一个字段(例如要使用的名称)。

当然,我想返回对象的实际表示形式作为资源...我遇到的问题是,如果我有一个可以通过 /foo/{id}/ 访问的 Foo 类型的对象,但它也与对象类型 Bar 作为 Foo 的一部分——我不想在响应中序列化 Bar。我想返回 Bar 资源的 URI。

我是不是只能靠自己了——没有框架处理这部分?尤其是在路径方面。感觉就像我必须创建用 @Path 注释的 BarResource,然后在序列化期间,设置从常量读取的 Bar POJO 引用的 URI。我无法获取类型并查找资源(以及随后的 @Path 注释),但这会很方便,不是吗?

I have a SpringServlet (from Jersey) that is exposing my JPA-annotated POJOs in a very basic manner right now. For example, rather than returning an actual represetantion of the object, I've just returned a field such as Name to play around.

Of course I want to return the actual representation of the object as a resource... the part where I am stuck is that if I have an object of type Foo accessible via /foo/{id}/ but it also has a relation to object type Bar as part of Foo -- I don't want to serialize Bar in the response. I want to return a URI to a Bar resource.

Am I on my own from here -- no frameworks handle that part? Especially with regard to the path. It feels like I'm going to have to create BarResource annotated with @Path but then also, during serialization, set the URI for the Bar POJO reference read from a constant. I'm not able to take a type and lookup a resource (and subsequently the @Path annotation) but that would be handy, no?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2024-10-11 20:41:30

在弄清楚资源、表示和对象之间的区别之前,你注定会感到困惑。

资源是一个概念,或者是对客户端应用程序有用的某种“事物”。这是非常模糊的。它由 URI 标识并使用方法进行操作。它需要模糊/灵活来弥补非常有限的方法集。

表示是一组可以通过线路传输的字节,将资源表示为某个时刻。

对象是一个实现细节,可能与资源直接相关,也可能没有。资源中包含的信息可以由单个对象或整个对象图来实现。资源的内容可以存储在文件中、SQL 查询的结果、XSLT 转换等等。

您可能会也可能不会使用对象序列化来创建资源的表示。对象之间的引用可能涉及单个资源内的内容或资源之间的链接。但是,将域对象序列化为表示时要非常小心。这肯定会在您的客户端和服务器之间引入一定程度的耦合,您将为此感到遗憾。

You are destined to remain confused until you clear up the distinction between resources, representations and objects.

A resource is a concept, or some "thing" that is useful to the client application. It is very nebulous. It is identified by a URI and is operated on using methods. It needs to be nebulous/flexible to make up for the very limited set of methods.

A representation is a set of bytes that can be transferred across the wire that represents the resource as some instant in time.

An object is an implementation detail that may or may not have a direct correlation with a resource. The information contained within a resource may be implemented by a single object or an entire object graph. A resource's contents could be stored in a file, the result of a SQL query, a XSLT transformation, pretty much anything.

You may or many not use object serialization to create representations of resources. References between objects may relate to content within a single resource or links between resources. However, be very careful serializing domain objects into representations. That is sure to introduce a level of coupling between your client and server that you will live to regret.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文