athieriot / jexter   0.5

GitHub

Spray server for dynamic, fake REST data

Scala versions: 2.10

Travis CI Status

Jexter

Spray server for dynamic, fake REST data

This project intent to be a lightweight server to provide a fake REST interface for you application.

Usage

Drop a file into a data/ directory in your classpath and it will be automatically served.

This is best deployed into a WAR file.

Static files

Any file present will be made available as is. The content type is deduced from the extension.

data/your/file.json will be available at http://localhost:port/data/your/file.json as application/json

Content Negociation

You can request, via the header value "Content-Type" a specific result format. This will order Jexter to look for a specific file extension in your classpath.

For example:

data/your/file.json can be available at http://localhost:port/data/your/file if you add in your header Content-Type = "application/json"

Dynamic templates

Ok. This is very good but I don't need Spray just for static files!

When faking API results, you quickly need to handle parameters. More generally, it might be handy to customise the data, generate values and so on.

Jexter allows you to use Scalate templates.

data/your/dyno.json.mustache will be available at http://localhost:port/data/your/dyno.json

Any parameters you have in your request is passed to the template as value.

Given a template:

{
    "title": "{{ceci}}"
}

http://localhost:port/data/your/dyno.json?ceci=cela will render:

{
    "title": "cela"
}

More features coming

https://github.com/athieriot/jexter/blob/master/TODO

Installation

Add the dependency:

SBT:

libraryDependencies += "com.github.athieriot" %% "jexter" % "0.5.1"

Maven:

<dependency>
    <groupId>com.github.athieriot</groupId>
    <artifactId>jexter_2.10</artifactId>
    <version>0.5.1</version>
</dependency>

Add a new web.xml file in webapp/WEB-INF with this content:

<?xml version="1.0"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_4.dtd">

<web-app>

    <listener>
        <listener-class>spray.servlet.Initializer</listener-class>
    </listener>

    <servlet>
        <servlet-name>SprayConnectorServlet</servlet-name>
        <servlet-class>spray.servlet.Servlet30ConnectorServlet</servlet-class>
        <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
        <servlet-name>SprayConnectorServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

For SBT, you will want to configure the xsbt-web-plugin

Examples

This project contains two minimal projects examples to start with:

Maven: https://github.com/athieriot/jexter/tree/master/examples/maven

SBT: https://github.com/athieriot/jexter/tree/master/examples/sbt

Configuration

You can optionally override values in an application.conf present in the classpath

Spray

If you deploy the project as a WAR at a different context than root. You need to specify it to Spray. In this example, Spray can respond to http://localhost:8080/maven

spray.servlet {
  root-path = "/maven"
}

Jexter

The root path of Jexter is where your files can be found in the class path.

jexter {
    rootPath = "data"
}

Troubleshooting

With the default JVM configuration, you might encounter an out of memory exception using Jexter. Increase the value of the property MaxPermSize should resolve the problem.

The JAVA_OPTS option to do so is:

-XX:MaxPermSize=256M

Thanks

Inspired from Dyson

Bitdeli Badge