Json views and jsonp

146 views
Skip to first unread message

manos

unread,
Nov 19, 2012, 9:59:28 PM11/19/12
to resthub-dev
First of all many thanks, resthub rocks in a just-feels-right kind of
way. I stumbled on it last week while trying to figure out the
plumbing involved in creating a generic service; i don't have
extensive experience with spring and couldn't figure out why nobody
was doing it. Bumped on resthub, messed around for half an hour, then
scraped all my code and just moved over my model classes and bits to
it overnight. I enjoyed it.

Right now I could use some hints on how to approach two issues the
right way to keep this up:

A) How to properly use Json views [1] or anything similar with
resthub, i.e. how do you folks approach the task of exposing different
subsets of an entity's members for different CRUD use-cases? For
example, consider an admin editing a subset of a user's profile VS the
same user editing his own "extended" profile.

Mostly concerned about the spring stack on this but wouldn't mind
pointers including backbone.js. It's pretty new for me but makes sense
nevertheless.

B) Any jsonp related app-wide magic specific to resthub i could tap
on? When i started with it a few days ago, i just threw a servlet
filter to add json padding to my controller responses. My intention
was to just hook those to some jquery client i was going to probably
painfully patch up, work on security stuff like tokens and so on.
However, after checking out the backbone.js stack i feel i'm missing
some big opportunity here. Sorry if this sounds stupid but is there a
way to make, for example, the "RESThub Backbone Stack tutorial" [2]
work over jsonp? Any non-obvious gems on this? The app i'm working on
is completely cross-domain so this would really be something.

[1] http://wiki.fasterxml.com/JacksonJsonViews
[2] http://resthub.org/backbone-stack.html#tutorial

Cheers,

Manos

Brian

unread,
Nov 20, 2012, 3:51:57 AM11/20/12
to resth...@googlegroups.com
Hello there

Thanks for using RESThub!
The 2.0 version is on its way (pretty close actually) and will be published on maven central, to be even more productive!

Regarding question A)

I've been looking at JsonViews and JsonFilters, but it seems like you have to manipulate your jackson objectmapper in your controller, thus reducing separation of concerns. But these are interesting Jackson features I would like to use in SpringMVC.

Another solution (the one we're recommending for now) would be using DTOs, especially if you'll have concerns similar to dje-rom (see this issue).
In two steps:
  1. using a SluggableSampleServiceBasedRestController
  2. mapping your model to DTOs
This may look tedious, but:
  • all the boring plumbing work can be done using ModelMapper (I'll write a bit on it in RESThub's documentation)
  • this decorrelates your "technical model" (persisted to your database) from the data you want to communicate to your clients - you can achieve better reusability and handle business changes better.
Anyway, if you're looking into JSONP, you may also look into Hypermedia later, so using DTOs is a pretty good idea anyway.

Regarding question B), I'll talk about it with my fellow RESThub developers (or one of them may answer you before).

Later,

-- Brian



--




Loïc Frering

unread,
Nov 20, 2012, 4:11:25 AM11/20/12
to resth...@googlegroups.com
Hi Manos,

Concerning your 2nd question about JSONP, as you might know, backbone relies on jQuery (or Zepto) for issuing AJAX requests so you can use jQuery out of the box support for JSONP. All you need to do is adding a `callback=?` at the end of the url in your Backbone Model or Collection. Then jQuery will detect that you want to use JSONP and will manage all the JSONP callback workflow transparently for you (it will generate a callback function).

You can have a look at this example where I use GitHub's API via JSONP. Feel free to come back to us if you encounter difficulties in enabling JSONP support on your Resthub-Backbone application.

Regards.


On Tue, Nov 20, 2012 at 3:59 AM, manos <ma...@abiss.gr> wrote:

manos

unread,
Dec 1, 2012, 12:25:00 AM12/1/12
to resthub-dev
Haven't used DTOs up to now as I was doing generic CRUD stuff to get
my backbone UI going, but i need to get to it. There's an itch for me
in json view annotations (it feels like "the resthub way" ;-) but I'm
unable to even evaluate the plumbing involved and the effort required
to do it :-/

Just to verify, I assume all CRUD URLs are transparent to jquery or it
will in any case have no issue patching up a jsonp GET for any of
them? I have a servlet filter in my resthub app that will just wrap
the request to use the proper HTTP method and all. Perhaps something
like this should be added in resthub?

Manos

Seb

unread,
Dec 5, 2012, 10:56:06 AM12/5/12
to resth...@googlegroups.com
Hi,

JsonView are indeed a nice way to customize serialization output without creating DTO and keep using RESThub generic classes.
Sadely, this is not supported by Spring MVC 3.1 or RESThub 2.0 out of the box, since a little bit of plumbing is needed in Jackson/SpringMVC integration.

A great article describe the right way (from my POV) to manage this :

I think it is easy to implement and very useful, so I created an issue for RESThub 2.1 in order to implement the @ResponseView + @JsonView annotations support described in previous blog post.

If you need this quickly, feel free to contribute it as described in the article into resthub-spring-stack, we will review the pullrequest and integrate into RESThub  2.1.0-SNAPSHOT.

Regards

Seb

unread,
Dec 11, 2012, 10:54:20 AM12/11/12
to resth...@googlegroups.com
Based on previously mentionned article, I have implemented a custom Json View support in RESThub, you can have a look to this pull request https://github.com/resthub/resthub-spring-stack/pull/159 and give us some feedbacks.

It will be shortly reviewed and merged in main GIT repository and available from RESThub snapshot repository as 2.1-SNAPSHOT version
Reply all
Reply to author
Forward
0 new messages