resthub / resthub-spring-stack

RESThub Spring stack
http://resthub.org/spring-stack.html
Other
121 stars 66 forks source link

Json serialization of Page<T> when the web service is annoted with a reponseView #209

Closed Brictarus closed 10 years ago

Brictarus commented 11 years ago

If the returned type of a controller method is a Page (T is a concrete type), and that method use a ResponseView to serialize its result, the returned value is always an empty object "{ }"

bclozel commented 11 years ago

I've done some thinking about this. We get "{}" as a response because the custom view doesn't apply to any field in the Page/PageResponse classes.

It looks like, by design, a ObjectWriter can only have a single active view at a given time. So we can't add another view in the object writer and get Page/PageResponse's fields serialized.

I thought about two possible solutions.

We can use custom PropertyFilters and Serializers to solve this issue in a more generic way (see this SO question). This approach is a bit complex and could need a fair amount of code and "instanceof" testing in the framework.

We can leverage views composition/inheritance as advertised on Jackson website. Views are applied on the whole hierarchy of objects, so we could provide a PageResponseView and annotate PageResponse attributes accordingly. Developers would have to extend this view in their view hierarchy to get this working.

Does that make sense to you?

sdeleuze commented 11 years ago

I am fine with your 2nd proposal

jripault commented 11 years ago

The cons of this method is that you have to recreate a PageResponseView from the PageImpl. Maybe there is something to do with Spring JPA by overriding the base repository or adding the methods returning PageResponseView

sdeleuze commented 11 years ago

I already had to create a custom PageResponse in RESThub since some fields were not accessible for serialization : https://github.com/resthub/resthub-spring-stack/blob/master/resthub-web/resthub-web-common/src/main/java/org/resthub/web/PageResponse.java

So I think we could just annotate the already existing PageResponsehttps://github.com/resthub/resthub-spring-stack/blob/master/resthub-web/resthub-web-common/src/main/java/org/resthub/web/PageResponse.java to make it works.

Any thoughts ?

On Tue, Aug 6, 2013 at 10:26 AM, Julien Ripault notifications@github.comwrote:

The cons of this method is that you have to recreate a PageResponseView from the PageImpl. Maybe there is something to do with Spring JPA by overriding the base repository or adding the methods returning PageResponseView

— Reply to this email directly or view it on GitHubhttps://github.com/resthub/resthub-spring-stack/issues/209#issuecomment-22164446 .

bclozel commented 11 years ago

@sdeleuze That's what I had in mind. @jripault the PageResponse class is in the web-common module, so the view class should ship with it as an inner class.

To make this work, we have to be sure that the TypeMapping Page -> PageResponse takes places at the right moment and doesn't conflict with our view strategy. I'm not really familiar with Jackson's way of executing those extensions.