开发者

What should I be doing to serialize/marshal (and vice-versa) resources that refer to other resources?

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 开发者_Go百科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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜