Evaluating Routes with Args

69 views
Skip to first unread message

Isa Goksu

unread,
Jun 29, 2012, 1:49:33 AM6/29/12
to resthub-dev
Hi folks,

I'm trying to use springmvc-router in one of our projects here. I've
having hard time evaluating routes programatically within a
controller. Here is a sample code:

My route file is say:
GET /hello/{id} controller.do
GET /hello controller.dont


public String do(@PathVariable String id) {

// id is 123

for (Router.Route route : Router.routes) {
if (route.getPath().startsWith("/hello")) {
list.add(route.path);
}
}

/* here my list contains:
/hello,
/hello/{id}
*/
}

I was hoping Router would do the evaluation out-of-the-box with the
given args. Then again, it doesn't do this way. How do I force Router
to evaluate paths with the params, something like:

/hello
/hello/123

I found this workaround, but I was wondering if there is an easy way
to do this:

Map<String, Object> args = new HashMap<String, Object>();

for (int i = 0; i < route.getArgs().size(); i++) {
args.put(route.getArgs().get(i).getName(), params[i]);
}

for (Router.Route route : Router.routes) {
if (route.getPath().startsWith("/hello")) {
Router.ActionDefinition action =
Router.reverse(route.action, args);

list.add(action.url);
}
}

Any other suggestions?

Brian

unread,
Jun 29, 2012, 5:27:38 PM6/29/12
to resth...@googlegroups.com
Hi Isa,

I think you're right. Springmvc-router is all about routing requests and reverse routing within views. So this use case doesn't fit well - for now. 

I've been looking into hypermedia myself and I'd like to figure something out for this router and springmvc.

Because you want to expose hypermedia links in your response headers, you want to generate links to:
- the current resource (but different action)
- other resources as well

Am I right?

I'll keep you posted. 


--
-- Brian

Isa Goksu

unread,
Jun 29, 2012, 5:59:23 PM6/29/12
to resth...@googlegroups.com
To do real Hypermedia APIs, we should be exposing all the resource related actions to the user. This is only true for that particular resource. It should not expose other resources. So in this case, say you have a resource for Person entity in your system. Assuming you have the necessary implementations, I'd expect following routes or similar to be exposed:

GET /person/{id}           personController.fetch
POST /person              personController.create
PUT /person/{id}           personController.update
DELETE /person/{id}     personController.delete

And, theoretically speaking, when you get the resource in XML:

<person>
   <id>123</id>
   <name>john</name>
   <lastname>doe</lastname>
   <links>
      <link rel="create" ref="/person" method="POST" />
      <link rel="delete" ref="/person/123" method="DELETE" />
      <link rel="update" ref="/person/123" method="PUT" />
   </links>
</person>

and in JSON:

{
   "id": 123, 
   "name": "john",
   "lastname": "doe",
   "links": [
     {"rel": "create", "ref": "/person", "method": "POST"},
     {"rel": "delete", "ref": "/person/123", "method": "DELETE"},
     {"rel": "create", "ref": "/person/123", "method": "PUT"},
   ]
}

Now clients can implement relying on the logic created based on these links rather than hardcoding these links into the client application. It'd be really nice to see this in the springmvc-router too.. I can implement some logic and check in if you want... I just didn't go too deep into the source code yet.. 

Or if you do so, I'd appreciate that ;)

Cheers,
Isa

Brian

unread,
Jun 30, 2012, 6:11:48 AM6/30/12
to resth...@googlegroups.com
To achieve that, several features are required:
- separation between hypermedia resource and the actual model
- "representers" or "decorators" to decorate resources when needed (if content-type negotiation points to HAL, for example)
- a clean, fluent API to build rel/pagination URIs

I think Spring-HATEOAS is going very well and fits those requirements.

I guess I have to implement a custom version of a ControllerLinkBuilder.
I've created an issue scheduled for 0.8 (for now).

-- Brian

Brian

unread,
Dec 28, 2012, 4:06:57 PM12/28/12
to resth...@googlegroups.com
For the record, this issue has been solved with a PR and is implemented in the latest snapshot 1.0.1-SNAPSHOT.

This feature will ship in the next version.

-- Brian
Reply all
Reply to author
Forward
0 new messages