Alex
Alex

Reputation: 101

Tapestry + REST

I want to add REST to my tapestry project, and so need to know how to implement it.

What is the better way ?

thx.

[Edit, copied from answer:] I have to add GET, PUT, POST and DELETE services to my tapestry application. I see that Tapestry has RESTful url but what about JAX-RS and annotations?

Upvotes: 10

Views: 6541

Answers (3)

user6708591
user6708591

Reputation: 356

Support for REST endpoints is a built-in feature since Tapestry 5.8.0. Writing and endpoint is pretty much the same as writing an onActivate() method in a page class.

Example:

Object onHttpGet(String name, BigDecimal lat, BigDecimal lon) {
    Waypoint w = new Waypoint(lat, lon, name);
    waypointService.save(w);
    return HttpStatus.created();
}

All the good Tapestry stuff like parameter type coercion, request processing chains etc. is available, so one can easily add REST support to existing Tapestry webapps. JAX-RS skills are not required, since it's not a JAX-RS implementation.

See the Tapestry Docs for more details.

Upvotes: 0

Henning
Henning

Reputation: 16349

You could use the Restlet API or any other JAX-RS implementation that can run as a servlet.

To have the web service co-exist nicely with Tapestry, there is one thing you have to configure in your Tapestry application module:

/**
 * Keep Tapestry from processing requests to the web service path.
 * 
 * @param configuration {@link Configuration}
 */
public static void contributeIgnoredPathsFilter(
        final Configuration<String> configuration) {
    configuration.add("/ws/.*");
}

This snippet tells the Tapestry filter not to handle requests to the /ws/ path where the web service is located.

Here's a snippet showing what your web.xml should approximately look like with Tapestry plus a Restlet Servlet:

<filter>
    <filter-name>app</filter-name>
    <filter-class>org.apache.tapestry5.spring.TapestrySpringFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>app</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Restlet adapter -->
<servlet>
    <servlet-name>WebService</servlet-name>
    <servlet-class>
        com.noelios.restlet.ext.spring.SpringServerServlet
    </servlet-class>

    ...
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>WebService</servlet-name>
    <!-- This path must also be set in AppModule#contributeIgnoredPathsFilter,
        otherwise Tapestry, being a request filter, will try to handle 
        requests to this path. -->
    <url-pattern>/ws/*</url-pattern>
</servlet-mapping>

That should help you get started.

Upvotes: 11

Timo Westk&#228;mper
Timo Westk&#228;mper

Reputation: 22200

If you want integrate a REST web service into a Tapestry project then Tapestry's RESTful URLs are probably not enough.

It is possible to integrate RESTEasy into Tapestry via this Tynamo module. RESYEasy is JAX-RS compatible.

I haven't used RESTEasy with Tapestry, but with Spring 2.5, and it worked really well.

Upvotes: 10

Related Questions