aspose file tools*
The moose likes Spring and the fly likes RestFul API - links/hrefs to composite objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "RestFul API - links/hrefs to composite objects" Watch "RestFul API - links/hrefs to composite objects" New topic
Author

RestFul API - links/hrefs to composite objects

Mag Wells
Ranch Hand

Joined: Jun 02, 2010
Posts: 38
I am not sure how to implement this.

I have a url to access authors -
for example -- localhost:8080/spring-project/authors/

It retrieves the list of authors.

However, I don't want to retrieve all the book details.
What it should have is



However, because of my database layer being hibernate, I directly convert those using xstream to xml, I get all the details of the book.

Ideally, in the above code, url1 should be like

localhost:8080/spring-project/books/1


and url2 should be like

localhost:8080/spring-project/books/2


So, question is is there any mechanism that does that ? and how do I pass the ip/hostname and the resource for that ? Is there any book to follow ?
Mark Secrist
Ranch Hand

Joined: Jul 01, 2003
Posts: 89
One way I've done this is just using a JSP with JSTL... something like the following.



Of course this presumes you have some book list set as a request or session attribute and the book has properties like bookId and bookTitle.

I'm not sure why using hibernate would cause you to need to stream the data to XML unless the issue is that the Hibernate session closes before you get a chance to display the values (and you're using FetchType.LAZY). If this is the case, you can configure a Spring interceptor to keep the entity manager (or Session) open until the view renders. You'd put this in your bean config file.


This is for JPA but if you're just using straight Hibernate, you might use something like the following


Also, check out the following:
http://community.jboss.org/wiki/OpenSessioninView
http://www.paulcodding.com/blog/2008/01/21/using-the-opensessioninviewinterceptor-for-spring-hibernate3/
Mag Wells
Ranch Hand

Joined: Jun 02, 2010
Posts: 38
Thing is
1- I want to keep the UI elements and the database elements separate.
2 - For example, my database names can change at will and can be not so good for any one using the xml output.
3- I don't want to the jsp way, as I have configured spring rest service and my UI is complete javascript based.
Mark Secrist
Ranch Hand

Joined: Jul 01, 2003
Posts: 89
Ok, if you're doing this in the controller then you'll need to get a similar value from the HttpServletRequest object. I don't see any specific database object info being passed. The key thing you need to capture is the unique id of each book as that's the way you come back in. Maybe I'm misunderstanding the problem. I don't think you'll be able to directly use Xstream with the object, although I don't know much about Xstream.
Mag Wells
Ranch Hand

Joined: Jun 02, 2010
Posts: 38
Yes you are right. I do have the required Id.

Now here is the basic flow/sequence.

1- httprequest -- spring framework -- some Java Objects---> Dao ---> DB
2. DB --> hibernate POJO --> Business Object (Convert hibernate POJO to UI POJO )---> Controller ---> XStream
3. Now, when I convert the hibernate POJO, I have a convertor for every pojo.
So, for example there is a convertor for Author and Book POJO. When I am converting the Author POJO to UI POJO, it calls the Book Convertor.
Now, every convertor takes as input a hibernate POJO and outputs a UI POJO. So, for composite objects, how do I control what should be converted and what not. Passing flags etc. just seems noob.
Mag Wells
Ranch Hand

Joined: Jun 02, 2010
Posts: 38
Apart from that, I have to plug in the relative urls .. how do I do that ?
Mark Secrist
Ranch Hand

Joined: Jul 01, 2003
Posts: 89
Ok, I think I get it. What you really want when listing books for authors is just the book URL (stored or calculated). I don't know much about your domain model but if you are using converters to build up the POJO that ultimately goes back to the UI (via XML), couldn't you define a custom instance of Book (BookLite) that only fetches the URL? Or... on the BookConverter, maybe there's a way to ask for just BookLite? This might work unless you sometimes need an author to contain a list of detailed books.

I think ultimately, this boils down to a Hibernate / Pattern problem.
Mag Wells
Ranch Hand

Joined: Jun 02, 2010
Posts: 38
cool. Looks like something is working out. What pattern ? some keywords or topic or pattern info would be great.. to get me on right foot.
Mark Secrist
Ranch Hand

Joined: Jul 01, 2003
Posts: 89
Ok, I think there are two questions there..
1) For the URL, you simply access the HttpServletRequest object with the getContextPath method. That will give you the path you will append your book specific URL to. Typically, if you simply referenced a path as "/somepath" as a URL on a returned page, this would be considered from the same server (i.e. http://localhost:8080/somepath). Calling getContextPath returns the value that represents your web application context. From there, you just append your specific information (i.e. /books/1). Depending on where the URL is built, you may need to pass the value of the contextPath obtained from the controller to your service tier.

2) I didn't have a specific pattern in mind but it occurs to me that maybe one of the problems you are facing is needing a way of handling the transfer of the information in the database to the UI via the set of converters you employ. One pattern that is similar to this is the TransferObjectAssembler pattern (http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObjectAssembler.html).
Mag Wells
Ranch Hand

Joined: Jun 02, 2010
Posts: 38
Yep, will take a look at that pattern.

So by 1) you mean, I have assign or configure the urls of the composite resources in controllers ? Is there a way to do it in Business Object (BO) Layer ? I mean creating an interface that lists relative paths..


For example Book is at /books/
Author is at /authors/

etc.. and then convertor can plug it in. Basically, I employ convertors in BO layer. It is not exactly the UI that it outputs but an xml for rest service.
Mark Secrist
Ranch Hand

Joined: Jul 01, 2003
Posts: 89
That's basically what I meant in the last part of 1. You would extract the contextPath from the HttpServletRequest object and then pass it into your business object layer if that's where you are ultimately building your URLs at.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: RestFul API - links/hrefs to composite objects