creating hyperlinks in vraptor3

35 views
Skip to first unread message

konata

unread,
Dec 17, 2010, 1:38:49 PM12/17/10
to caelum-vraptor-en
Hi, I'm trying out vraptor3 with Java, and the creating hypermedia
links feature is not working. I tried
public void configureRelations(RelationBuilder builder) {
builder.relation("self").uses(ItemController.class).showItem(id);
builder.relation("basket").at("/Basket/add/"+id);
}
as suggested in tutorial, and no links created in the xml output. I
also tried the section 'Hypermedia resources' in http://blog.caelumobjects.com/tag/vraptor/
and all I get from Eclipse is 'The method relation(String) is
undefined for the type Restfulie'.

What are the steps to getting hypermedia links to work/to show up in
xml output? ie. to get output like
<item>
<id>3</id>
<name>Soap</name>
<price>1.99</price>
<atom:link rel="self" href="http://localhost:8080/items/3"
xmlns:atom="http://www.w3.org/2005/Atom"/>
<atom:link rel="basket" href="http://localhost:8080/baskets"
xmlns:atom="http://www.w3.org/2005/Atom"/>
</item>

Thanks,
Konata

Guilherme Silveira

unread,
Dec 17, 2010, 2:02:58 PM12/17/10
to caelum-v...@googlegroups.com
Hello Konata,

There is a configuration step on the web.xml file to set up the
serializer, its easy to miss that step (we should makeit the default
one). Did you configure the restfulei package on the web.xml file?

regards

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

Lucas Cavalcanti

unread,
Dec 17, 2010, 2:03:42 PM12/17/10
to caelum-v...@googlegroups.com
Hi,

you have to add this to your web.xml :
         <context-param>
<param-name>br.com.caelum.vraptor.packages</param-name>
<param-value>br.com.caelum.vraptor.restfulie</param-value>
</context-param>

and use at the end of your logic method (on the Controller)

result.use(representation()).from(item).serialize();

static import from Results.*

[]'s

konata

unread,
Dec 20, 2010, 9:01:03 AM12/20/10
to caelum-vraptor-en
Hi all,

thanks so much for your replies. I have configured the web.xml and
imported from Results. No luck.

This is what I have in my web.xml:
<context-param>
<param-name>br.com.caelum.vraptor.packages</param-name>
<param-value>br.com.caelum.vraptor.restfulie</param-value>
</context-param>
<filter>
<filter-name>vraptor</filter-name>
<filter-class>br.com.caelum.vraptor.VRaptor</filter-class>
</filter>
<filter-mapping>
<filter-name>vraptor</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>

In my basket controller:
@Path("/Basket")
public void basket() {
result.use(xml()).from(basket).include("list").serialize();
}

And in my basket object:
public void configureRelations(RelationBuilder builder) {
builder.relation("self").uses(BasketController.class).basket();
builder.relation("item").at("/Item/1");
}

Is the way I organize my code incorrect? Any other places that
requires attention?

Thanks so much again!
Konata

Lucas Cavalcanti

unread,
Dec 20, 2010, 10:09:17 AM12/20/10
to caelum-v...@googlegroups.com
does basket implement HypermediaResource?

konata

unread,
Dec 20, 2010, 4:47:34 PM12/20/10
to caelum-vraptor-en
yes, basket implements HypermediaResource.

Konata

Lucas Cavalcanti

unread,
Dec 20, 2010, 5:30:05 PM12/20/10
to caelum-v...@googlegroups.com
I've tried this and it worked:

also I'm using vraptor 3.2.0

[]'s

konata

unread,
Dec 21, 2010, 9:15:10 AM12/21/10
to caelum-vraptor-en
Thanks for your code. I have tried it and the output I got is
<basket>
<id>2</id>
<name>duh</name>
</basket>

So how do you get the links like
<atom:link rel="self" href="http://localhost:8080/items/3"
xmlns:atom="http://www.w3.org/2005/Atom"/>
?

I'm using vraptor 3.2.0 as well.

Thanks,
Konata

Lucas Cavalcanti

unread,
Dec 21, 2010, 9:20:47 AM12/21/10
to caelum-v...@googlegroups.com
try this:

-add the classes and web.xml from my gist: https://gist.github.com/749124
-run it on a server

my output:
<basket>
<id>123</id>
<name>duh</name>
<atom:link rel="self" href="http://localhost:8080/vraptor-blank-project/items/123" xmlns:atom="http://www.w3.org/2005/Atom"/>
<atom:link rel="basket" href="http://localhost:8080/vraptor-blank-project/Basket/add/123" xmlns:atom="http://www.w3.org/2005/Atom"/>
</basket>

[]'s

konata

unread,
Dec 21, 2010, 10:01:33 AM12/21/10
to caelum-vraptor-en
It works like a charm! Thanks so much!

Konata

On Dec 21, 9:20 am, Lucas Cavalcanti <lucasmrtu...@gmail.com> wrote:
> try this:
>
> -download the vraptor-blank-projecthttp://code.google.com/p/vraptor3/downloads/detail?name=vraptor-blank...
> -add the classes and web.xml from my gist:https://gist.github.com/749124
> -run it on a server
> -accesshttp://localhost:8080/vraptor-blank-project/items/123
Reply all
Reply to author
Forward
0 new messages