Kauri Documentation
 PreviousHomeNext 
2.9 Static resourcesBook Index2.10.2 About JAX-RS in Kauri

2.10 Dynamic resources

Dynamic resources can be created in several ways. One is using the Restlet API, another one is using the more standard and somewhat higher-level JAX-RS API. In the examples below, we will use JAX-RS.

2.10.1 Examples

2.10.1.1 Hello World

Suppose we want to create a "Hello, world!" resource. A resource is implemented by creating a Java class, which we call a resource class.

Assuming you created a project where the default router configuration and REST-service mounts are present, you can do so by creating a Java class, for example at:

module1/src/main/java/com/mycompany/module1/HelloWorldResource.java

The name and package of the class do not matter, you can take anything you like.

The HelloWorldResource.java can be implemented as follows:

package com.mycompany.module1;

import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;

@Path("helloworld")
public class HelloWorldResource {

    @GET
    @Produces("text/html")
    public String get() {
        return "<html><body>Hello, world!</body></html>";
    }
}

The JAX-RS API makes extensive use of annotations. By adding the annotation @Path("helloworld"), the resource will be available at this path in the URI space. GET-requests will be handled by the get() method because it has an @GET annotation. The method could have had any other name. There are no interfaces to implement, nor method naming conventions to be followed. Using these annotations, you can easily turn any Java object into a Web resource.

Your module jar will be automatically scanned for classes with JAX-RS annotations, so there is no central configuration to update (see also the notes on router configuration further on).

If you add the above class to your project, build it using "mvn install", and (re)start Kauri, you can access the resource at:

http://localhost:8888/helloworld

2.10.1.2 Personalized hello

Let us make the sample somewhat more interesting, by saying "Hello, {your name}!". For this, we want to address this personalized hello-resource using an URI path like "hello/Steven". The listing below shows how this is done.

package com.mycompany.module1;

import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.PathParam;

@Path("hello/{name}")
public class HelloWorldResource {

    @GET
    @Produces("text/html")
    public String get(@PathParam("name") String name) {
        return "<html><body>Hello," + name + "!</body></html>";
    }
}

To see this in action, recompile the project using "mvn install" and restart Kauri. Then go to:

http://localhost:8888/hello/Steven

The special syntax using the curly braces {} in the @Path annotation follows a specification called URI templates.

2.10.1.3 Hello via a template

Instead of using string concatenation to build the HTML, let us use a template instead.

The resource class becomes:

package com.mycompany.module1;

import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.PathParam;

import org.kauriproject.representation.build.KauriRepresentation;

@Path("hello/{name}")
public class HelloWorldResource {

    @GET
    @Produces("text/html")
    public KauriRepresentation get(@PathParam("name") String name) {
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("name", name);
        return new KauriRepresentation("hello", data);
    }
}

The template file should be defined at:

src/main/kauri/templates/hello.xml

The name of the template corresponds to the first argument of the KauriRepresentation constructor, plus xml extension.

Its content could be as follows:

<?xml version="1.0"?>
<html>
  <body>Hello, ${name}!</body>
</html>
 PreviousHomeNext 
2.9 Static resources2.10.2 About JAX-RS in Kauri