Skip to content
This repository has been archived by the owner on Aug 24, 2023. It is now read-only.

gServ Framework

Lee Collins edited this page Mar 22, 2016 · 17 revisions

gServ Framework provides a container-free way to expose new or existing functionality as a REST based service.

Getting Started

  • Using Maven Add the following dependency to your pom.xml file
<dependency>
        <groupId>io.github.javaconductor</groupId>
        <artifactId>gserv</artifactId>
        <version>1.0.0</version>
        <type>jar</type>
</dependency>

Also, add the following repository reference to your repositiories element:

    <repositories>
        .
        <repository>
          <id>jCenter</id>
          <name>Bintray jCenter Repository </name>
          <layout>default</layout>
          <url>http://jcenter.bintray.com</url>
        </repository>
        .
    </repositories>
  • Using Gradle Add the following dependency to your build.gradle file
compile(group: 'io.github.javaconductor', name: 'gserv', version: '1.0.0', ext: 'jar')

Also, add the following repository reference:

repositories {
    jcenter() 
}

Creating REST Resources:

Creating REST resources is easy to do. Either use the static or instance methods:

GServ.Resource(path, resourceDefinition)
new GServ().resource(path, resourceDefinition)
The path argument contains the path prefix for this resource
The resourceDefinition argument is a Closure defining the available actions for resource

Here, we will create a Books REST resource.

def gserv = new GServ()

/// Create a Books REST resource

/* the root path is passed to the GServ.resource() method
along with a closure defining the available actions of the resource
*/
def bookService = ...

def bkResource = gserv.resource("/books") {
      ////
      // responds  to /books/faq
      get "/faq", file("text/html", "BooksFaq.html")
      // responds  to /books/xyz
      get ":id", { id ->
          def book = bookService.get( id )
          writeJson book
      }
      // responds  to /books/all
      get "/all", {  ->
        def books = bookService.allBooks ()
        header "content-type", "application/json"
        writeJSON books
    }
}

After this call, bkResource represents a resource with root '/books' and 3 actions:

  1. GET path='/books/faq'
  2. GET path='/books/all'
  3. GET path='/books/:id' (where 'id' could be a book ID.)

This resource can now be added to a Service.

Composing resources into Services

Services are made of Resources, additional actions and Config Settings. Creating services in gServ is extremely simple. The following code creates an HTTP server and starts it listening to port 8080:

// The http() method creates a GServInstance
gserv.http {
    // setup a directory for static files
    static_root "webapp"

    useResources true
    /// add Book REST resources to our GServ instance
    resource bkResource 
}.start(8080);

In addition to serving the Books Resource, we also serve static docs from the folder 'webapp'. This folder can be on the file-system or on the classpath. The above call to useResourceDocs(true) tells gServ to look on the classpath (classpath:/docs) as well as the "webapp" folder for static resources.