-
Notifications
You must be signed in to change notification settings - Fork 9
gServ Framework
gServ Framework provides a container-free way to expose new or existing functionality as a REST based service.
- 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 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:
- GET path='/books/faq'
- GET path='/books/all'
- GET path='/books/:id' (where 'id' could be a book ID.)
This resource can now be added to a Service.
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.
gServ © 2014-2017 Lee Collins. All rights reserved.